serialization :reading and writing objects

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

Hello all, In this tutorial we are going to learn about java serialization. We are going to take overview of java serialization.

What is Serialization and de-serialization?

  • Serialization is the process of writing the state of the object into a file.
  • De-serialization is the process of reading the object from a file which is already written using serialization

Why to do it? Whats the need?

Sometimes we have a requirement that the state of the object needs to be written somewhere and using that file the object needs to be created from the previously written state; for example we may have to pass the object information over the network and read it there on the other side. Java serialization API provides that.

How to serialize?

  • First, implement the marker interface
  • By doing this, our object is eligible for serialization
  • Create an object and serialize it into some file

Consider we have two classes Bike and TestSerialization. TestSerialization class is used to write Bike object into a file.

Now when we run this program, you can see the file serTest.ser is created at the specifed location. Now we want to test whether the the object is written properly in the file or now. Add this method to your TestSerialization class.

To read the file, add this to mainmethod

Notice the line deSerializedBike = (Bike) readSerializedObject("D:\\serTest.ser"); . We need a cast as it returns Object .

Complete code becomes,

Run this program and the output is

So, what happened here?

  • We have written the bike object into serTest.ser file using writeObject("D:\\serTest.ser", bike);
  • Created another Bikereference variable Bike deSerializedBike = null;
  • Read serialized object and assigned using deSerializedBike = (Bike) readSerializedObject("D:\\serTest.ser");
  • Printing the deSerializedBike object gave us the state of bike object which was serialized in serTest.ser
Share Button

Prasad Kharkar

Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

9 thoughts on “serialization :reading and writing objects

  • Pingback:JAXB: Marshalling and Unmarshalling java objects to xml | theJavaGeek

  • November 21, 2013 at 1:39 am

    Thank you, superb , i got it sir. it best for me .

    • November 21, 2013 at 10:40 am

      I am glad it helped you.

  • January 15, 2014 at 8:17 pm

    Explained in really simple language !!!
    Cool !! Keep it up buddy !!!

  • August 31, 2014 at 5:11 pm

    you did not close stream objects 🙂 ,,,

    • September 3, 2014 at 11:43 am

      Hi Vikas, thank you for pointing out. Actually I just wanted to demonstrate serialization and deserialization.

  • January 2, 2015 at 2:42 pm

    Can you also explain a little more about serialization? What about subclasses which extend serialized objects? Or when we have references of Classes as attributes which we don’t want serialized? For ex. What if this Bike class has a reference of say class ‘Gear’ which we don’t want serialized.

    The reason I’m asking is because not all web servers are serialized-class friendly. We might face certain problems after deployment. An explanation here would be great help!

  • June 27, 2016 at 1:58 am

    Serialization provides us deep copy.


Leave a Reply

Your email address will not be published. Required fields are marked *