01 February 2013

Java Serialization


Java provides a mechanism, called object serialization where an object can be represented as a sequence of bytes that includes the object's data as well as information about the object's type and the types of data stored in the object. After a serialized object has been written into a file, it can be read from the file and deserialized that is, the type information and bytes that represent the object and its data can be used to recreate the object in memory.

Most impressive is that the entire process is JVM independent, meaning an object can be serialized on one platform and deserialized on an entirely different platform. Classes ObjectInputStream and ObjectOutputStream are high-level streams that contain the methods for serializing and deserializing an object. 

The ObjectOutputStream class contains many write methods for writing various data types, but one method in particular stands out:

public final void writeObject(Object x) throws IOException

The above method serializes an Object and sends it to the output stream. Similarly, the ObjectInputStream class contains the following method for deserializing an object:

public final Object readObject() throws IOException, ClassNotFoundException

Notice that for a class to be serialized successfully, two conditions must be met:
----------------------------------------------------------------------------------
The class must implement the java.io.Serializable interface.
All of the fields in the class must be serializable. If a field is not serializable, it must be marked transient.

Example:


// Example of serialiazation and deserialization of objects in java

import java.io.*;
class Employee implements java.io.Serializable
{
String name;
String empcode;
int sal;
String BUName;
public void printEmp()
{
System.out.println("Employee Name:"+name);
System.out.println("Employee Emp Code:"+empcode);
System.out.println("Employee Name:"+sal);
System.out.println("Employee Name:"+BUName);

}

}

public class Test14 {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
// stored the data using serialization
FileOutputStream f1 = new FileOutputStream("abc.ser");
ObjectOutputStream o1 = new ObjectOutputStream(f1);
Employee emp1 = new Employee();
Employee emp2 = new Employee();
emp1.name = "Madhu Sudhan";
emp1.empcode = "E2030";
emp1.BUName = "VSA";
emp1.sal = 90000;
emp2.name = "Radha Krishna";
emp2.empcode = "E2031";
emp2.BUName = "ST";
emp2.sal = 100000;
o1.writeObject(emp1);
o1.writeObject(emp2);
o1.close();
f1.close();
//
System.out.println("Enter emp code to search");
InputStreamReader i1 = new InputStreamReader(System.in);
BufferedReader b1 = new BufferedReader(i1);
String x = b1.readLine();
// reading serialized object from the file

FileInputStream fi = new FileInputStream("abc.ser");
ObjectInputStream oi = new ObjectInputStream(fi);
Employee etest = (Employee) oi.readObject();
if (etest.empcode.equals(x))

etest.printEmp();

else {
Employee etest1 = (Employee) oi.readObject();
if (etest1.empcode.equals(x))
etest1.printEmp();
}
fi.close();
oi.close();

}catch(FileNotFoundException e)
{
e.printStackTrace();
}
catch(IOException e2)
{
e2.printStackTrace();
}
catch(ClassNotFoundException e3)
{
e3.printStackTrace();
}

}

}


No comments:

Post a Comment