Thursday, October 11, 2007

Java Faqs - How do I deserilaize an Object?

Answer : To deserialize an object, perform the following steps:
- Open an input stream -
- Chain it with the ObjectInputStream
- Call the method readObject() and cast the returned object to the class that is being deserialized.
- Close the streams
Java Code
try{
fIn= new FileInputStream("c:\\emp.ser");
in = new ObjectInputStream(fIn);
//de-serializing employee
Employee emp = (Employee) in.readObject();
System.out.println("Deserialized " + emp.fName + " " + emp.lName + " from emp.ser ");

}catch(IOException e){
e.printStackTrace();
}catch(ClassNotFoundException e){
e.printStackTrace(); }

No comments: