by Dinesh Thakur Category: Stream

Serialization is the technique most commonly used for network applications and persistent storage of objects, in order to maintain the reference of a remote object.


Storing the state of the object into a file is often referred to as serialization. Whereas, reading the object state from a stored file is referred to as deserialization.

The streams Object Output Stream and the Object input Stream are used for object serialization and deserialization, respectively. These classes allow us to read/write objects from to stream converting from internal to external 8-bit representation. These two classes throw the exceptions IOException and StreamCorruptedException.

The two methods that are useful for object serialization are read Object() on ObjectinputStream and writeObject(Object obi) on the ObjectOutputStream. To make any object serializable, the object of the class must implement the serializable interface of the package. That is, we have to specify implements Serializable in the definition of the class, the objects of which we would like to read/write from to files.

Any variable in a class having the transient modifier will not be saved in the file (that is, that variable is not serializable). Similarly static variables are not serializable. There is an inter face called Externalizable, which is used to control the serialization process of the object.

Program implementing the Serialization Example class illustrates the use of the object serialization, which stores the object state in a file called object.txt and desterilizes it.

Program Using object serialization and deserialization.


class ObjectSerialization implements Serializable


      String Employeename;

      String designation;

      ObjectSerialization (String number,String designation)



          this.designation =designation;


      public String toString()


           System.out.println("Employee name is;"+Employeename);

           System.out.println("Designation is:"+designation);



public class SerializationExample


      public static void main(String args[ ])




                 ObjectSerialization obj = new ObjectSerialization ("Kumar",


                 FileOutputStream fos=new FileOutputStream("object.txt");

                 ObjectOutputStream oos=new ObjectOutputStream(fos);




                 FilelnputStream fis=new FilelnputStream("object.txt");

                 ObjectinputStream ois=new ObjectinputStream(fis);

                 ObjectSerialization obj2=new (ObjectSerialization) ois.readObject()

                 System.out.println("value of the obj2: "+obj2);


             catch(NotSerializableException e1)


                 System.out.println("ioexception has occurred:"+e 1.getMessage());


             catch(StreamCorruptedException e2)


            System.out.println("ioexception has occurred:"+e2.getMessage());


            catch(IOException e3)


                 System.out.println("ioexception has occurred;"+e3.getMessage());




The output of Program is the following:

          Value of the obj2;

          Employee name is; Kumar

          Designation: Project Leader

File input Stream and File Output Stream are used to read/write the object from/to a file respectively. In Program when we have problems with stream classes, the exceptions caught by the try block may be IO Exception or Stream Corrupted Exception. The exception Not Serializable Exception will arise if the class that is going to be serializable does not implement the serialization interface.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles