Home Java Example Java Io Java Serialization
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Java Serialization

Advertisement
In this section, you will learn how to serialize and deserialize an object with the example code.

Java Serialization

In this section, you will learn how to serialize and deserialize  an object with the example code.

In Java, Object can be represented as sequence of byte which have data as well as information about states of object. Information about states of objects includes type of object and the data types stored in the object. Representing object into this form is known as Object Serialization.

And the recreation of object into the memory from this sequence of byte, is known as Deserialization.

Serialized object can be write into a file or can also store into data base.

Serialization & deserialization is independent of JVM. It means an object can be serialized on one platform and can also recreate or deserialized on a totally dissimilar platform.

The ObjectOutputStream  class is used to serialize a object and also contained methods associated with Serialization.

The ObjectInputStream class is used to deserialize a object and also contained methods associated with Deserialization.

Serializing a class

For serializing a class successfully, following things must be keep in mind :

  • The class must implements the java.io.Serializable interface.
  • If you want to avoid serialization of any field or any field is not serializable(Ex. Thread field), it must be declared using transient keyword as follows :

     public int transient secondaryMobile;

          Due to the above declaration secondaryMobile field will be ignored during serialization.

EXAMPLE 1: Serializing an object & storing it into a file

In the given below example, we are going to serialize a class Student and storing it into a file named as "student.ser".

Note :By Java convention, when we Serializing an object to a file, it must have extension .ser. For Example : student.ser

We are going to serialize the below class Student, it must implements java.io.Serializable interface :

public class Student implements java.io.Serializable{
public String name;
public String address;
public transient int rollno;
public int roomNo;
}

The ObjectOutputStream class is used to serialize an Object. The following SerializeExample program instantiates an Student object and serializes it to a file.

When the below code executed successfully, it will produce a file student.ser and gives output message . Given below the code of SerializeExample  class :

import java.io.*;

public class SerializeExample {
public static void main(String[] args) {
Student e = new Student();
e.name = "Kapil k Singh";
e.address = "E-247,Beta-1,Noida";
e.rollno = 513210153;
e.roomNo = 111;
try {
FileOutputStream fileOut = new FileOutputStream("student.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut);
out.writeObject(e);
out.close();
fileOut.close();
System.out
.println("Object is serialized & stored in 'student.ser'");
} catch (IOException ie) {
ie.printStackTrace();
}
}
}

It will produce the following output :

Object is serialized & stored in 'student.ser'              

EXAMPLE 2: Deserializing an Object:

Given below code will deserialize the student object created in the above(SerializeExample) code :

import java.io.*;

public class DeserializeExample {
public static void main(String[] args) {
Student e = null;
try {
FileInputStream fileIn = new FileInputStream("Student.ser");
ObjectInputStream in = new ObjectInputStream(fileIn);
e = (Student) in.readObject();
in.close();
fileIn.close();
} catch (IOException i) {
i.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Student class not found");
c.printStackTrace();
return;
}
System.out.println("Deserialized Student...");
System.out.println("Name: " + e.name);
System.out.println("Address: " + e.address);
System.out.println("Roll no: " + e.rollno);
System.out.println("Room No: " + e.roomNo);
}
}

The above code will produce the following output :

Deserialized Student...
Name: Kapil k Singh
Address: E-247,Beta-1,Noida                              
Roll no: 0
Room No: 111

In the above output, you can see that the Roll number field is showing 0 instead of 513210153. This is because, we set it transient. Due to this, it is not serialized and it will give default value 0(zero). In case of string it is null.

Download Source Code

EXAMPLE 3 : INSERT SERIALIZED OBJECT INTO DATABASE TABLE

Advertisements

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 1, 2011

Related Tutorials

Ask Questions?    Discuss: Java Serialization  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Mahesh
August 21, 2012
Clarification

while i am saving student class into student.ser the class not complied properly its throwing me an exception.so i tried to change the ext with .java its working fine.. please let me know why student.ser is not allowing to me complie..? Thanks for your example.. Mahesh
DMCA.com