Monday, April 12, 2010

Serialization and Deserialization

  1. Serialization is the process of transforming an in-memory object to a byte stream
  2. An object is serialized by writing it to an ObjectOutputStream. 
  3. Serialization Code 
           FileOutputStream out = new FileOutputStream("test.txt" );
           ObjectOutputStream oos = new ObjectOutputStream( out );
           oos.writeObject(new String ());
           oos.close ();
  1. Deserialization is the inverse process of reconstructing an object from a byte stream to the same state in which the object was previously serialized. 
  2. An object is deserialized by reading it from an ObjectInputStream.
  3. Deserialization Code
FileInputStream in = new FileInputStream( "test.txt" );
ObjectInputStream ois = new ObjectInputStream( in );
String s = (String) ois.readObject();
ois.close();



No comments:

Post a Comment