Name the class that used to read objects directly from a
stream?

Answers were Sorted based on User's Feedback



Name the class that used to read objects directly from a stream?..

Answer / ravi jian

Ans 2
is wrong in fact its apposite is true

i.e.
for reading ObjectInputStream

for writing ObjectOutputStream

Is This Answer Correct ?    4 Yes 0 No

Name the class that used to read objects directly from a stream?..

Answer / prasad

ObjectInputStream

Is This Answer Correct ?    4 Yes 1 No

Name the class that used to read objects directly from a stream?..

Answer / qim2010

ObjectInputStream is used for reading and ObjectOutPutSteam is
used for writing.

Here is a simple example:

import java.io.EOFException;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;

/**
 *
 * @author javadb.com
 */
public class Main {
    
    /**
     * Example method for using the ObjectInputStream class
     */
    public void readPersons(String filename) {
        
        ObjectInputStream inputStream = null;
        
        try {
            
            //Construct the ObjectInputStream object
            inputStream = new ObjectInputStream(new
FileInputStream(filename));
            
            Object obj = null;
            
            while ((obj = inputStream.readObject()) != null) {
                
                if (obj instanceof Person) {
                
                    System.out.println(((Person)obj).toString());
                }
                
            }
            
         
        } catch (EOFException ex) { //This exception will be
caught when EOF is reached
            System.out.println("End of file reached.");
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        } catch (FileNotFoundException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            //Close the ObjectInputStream
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new Main().readPersons("myFile.txt");
    }
}

Is This Answer Correct ?    0 Yes 0 No

Name the class that used to read objects directly from a stream?..

Answer / devarathnam

Hi... ObjectOutPutSteam and ObjectInputStream are used for
readign and writing.

Is This Answer Correct ?    1 Yes 4 No

Post New Answer

More Core Java Interview Questions

what is the difference b/w static and final methods?

1 Answers  


What is a map in java?

0 Answers  


What are internal and external variables?

0 Answers  


How many bits is a word?

0 Answers  


why using interface interface ?

0 Answers  






84. try { 85. ResourceConnection con = resourceFactory.getConnection(); 86. Results r = con.query(”GET INFO FROM CUSTOMER”); 87. info = r.getData(); 88. con.close(); 89. } catch (ResourceException re) { 90. errorLog.write(re.getMessage()); 91. } 92. return info; Which is true if a ResourceException is thrown on line 86? 1 Line 92 will not execute. 2 The connection will not be retrieved in line 85. 3 The resource connection will not be closed on line 88. 4 The enclosing method will throw an exception to its caller.

1 Answers  


What methods are used to get and set the text label displayed by a button object?

0 Answers  


can we write program without class

6 Answers   TCS,


How is Garbage collection done in Java?

3 Answers   T3 Softwares,


What is the difference between Error, defect,fault, failure and mistake?

0 Answers   HCL,


What are voids?

0 Answers  


How can we create an immutable class in java?

0 Answers  


Categories