Wednesday, October 8, 2014

Java Serialization

 java provides a mechanism, called "Object Serialization".
where an object can be represented as a sequence of byte that includes the objects data as well as all information about the objects
type and the type of data stored in the object.

After serialized object has been written into a file; with extension .ser
object can be read from the file and deserialized back to get real data of object.

for this mechanism java provides two classes
1) ObjectOutputStream
2) ObjectInputStream
both are high level streams that contain the methods for serialization and deserialization an object.
public final void writeObject(Object x) throws IOException
above method is in ObjectOutputStream class.

public final Object readObject(Object x) throws IOException,ClassNotFoundException
above method is from ObjectInputStream which returns the Object type.

Notice: while performing this serialization and Deserialization
two conditions must be met:
1. The class must be implement the java.io.Serializable interface.
2. All of the fields in the class must be serializable. if a field is not serializable it must be marked as transient keyword.
 Sample Example:

1.Student.java

public class Student implements java.io.Serializable{
public String name;
public String Address;
public transient int Rollno;
public int seatno;
public void SeatnoCheck()
{
System.out.println("Exam mail Check by "+name+" "+seatno);
}

}
//********************* Serialization*********************
2.StudentDataSer .java
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class StudentDataSer {
public static void main(String[] args) {
Student s = new Student();
s.name = "Sandy";
s.Address = "Nashik";
s.Rollno = 12;
s.seatno = 3245;
try {
FileOutputStream fileout = new FileOutputStream("E://Sandeep.ser");
ObjectOutputStream out = new ObjectOutputStream(fileout);
out.writeObject(s);
out.close();
fileout.close();
System.out
.println("Student data saved in file by serializing student object ");
} catch (IOException i) {
i.getMessage();
}
}

}
//********************* Deserialization**********************
3.StudentDataDeser.java

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

public class StudentDataDeser {
public static void main(String[] args) {
Student s = null;
try {
FileInputStream filein = new FileInputStream("E://Sandeep.ser");
ObjectInputStream in = new ObjectInputStream(filein);
s = (Student) in.readObject();
in.close();
filein.close();
} catch (IOException | ClassNotFoundException i) {
System.out.println("file not found to read");
i.printStackTrace();
return;
}
System.out.println("Desrealized file output is");
System.out.println("Student Name is " + s.name);
System.out.println("Student Address is " + s.Address);
System.out.println("Student Rollno is " + s.Rollno);
System.out.println("Student Exam Seat No is " + s.seatno);
}
}

out put when Deserialized:

Desrealized file output is
Student Name is Sandy
Student Address is Nashik
Student Rollno is 0
Student Exam Seat No is 3245

 
 

No comments:

Post a Comment