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 |
What is the method to expand and collapse nodes in a jtree?
Write a java program to find the route that connects between Red and Green Cells. General Rules for traversal 1. You can traverse from one cell to another vertically, horizontally or diagonally. 2. You cannot traverse through Black cells. 3. There should be only one Red and Green cell and at least one of each should be present. Otherwise the array is invalid. 4. You cannot revisit a cell that you have already traversed. 5. The maze need not be in the same as given in the above example
Name few "optional" classes introduced with java 8 ?
What is the difference between the paint() and repaint() methods in java programming?
what is finalmethod & final variable with example?
What is static and final keyword in java?
what are the design patterns in struts?
When the constructor of a class is invoked?
What are three types of loops in java?
What is an object?s lock? Give name of object?s that have locks?
What is object cloning in Java?
0 Answers SwanSoft Technologies,
Different types of Layouts?