Name the class that used to read objects directly from a
stream?
Answers were Sorted based on User's Feedback
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 |
Answer / devarathnam
Hi... ObjectOutPutSteam and ObjectInputStream are used for
readign and writing.
| Is This Answer Correct ? | 1 Yes | 4 No |
Describe what a thread-local variable is in java?
What is foreach loop in java?
what is aberivation of java?
Why map is used in java?
Why are lists ordered in java?
What is an abstract class and what is it’s purpose?
Package1 and Package2 both have a method name lets say "methodA" with different implementation. When I import both the packages in a java class how can I use both the methods?
What is the independent variable in an experiment?
What is the preferred size of a component in java programming?
what is the difference between HashMap And HashTable?
Can an arraylist be empty?
Which is the best way of exception handling?