Java Serialization
JAVA SERIALIZATION
Serialization is a process by which objects can be represented as a sequence of bytes. These bytes have information about object's data, object's type and datatypes of members in that objects.
We can write that object in a file and then it can be read and deserialized.
We retrieve the information from bytes and can recreate the object in the memory.
The most powerful feature is that the whole process is independent from JVM. This means that an object can be serialized form one machine and can be deserialized to another machine.
ObjectInputStream and ObjectOutputStream contains the methods for serialization and deserialization.
Important methods are:
public final void writeObject(Object x) throws IOException
The above method serializes an Object and then sends it to the output stream. Similarly, the ObjectInputStream class has the method for deserializing an object
public final Object readObject() throws IOException, ClassNotFoundException
This method retrieves the next Object out of the stream and deserializes it. The return value is Object, so we will need to cast it to its appropriate datatype.
The class that implements java.io.Serializable interface can be serialized and those fields which are marked transient cannot be serialized.
Example:
Person Class
public class Person implements Serializable { private static final long serialVersionUID = -3216968774585522982L; int age; String Address; public Person(){ System.out.println("Person Class constructor called"); } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getAddress() { return Address; } public void setAddress(String address) { Address = address; } public Person(int age, String address) { this.age = age; this.Address = address; } }
Employee Class
public class Employee extends Person implements Serializable { private static final long serialVersionUID = -2892927121614640166L; private String name; public Employee(String name2, int id2, int salary2) { super(5, "Delhi"); System.out.println("Employee Class constructor called"); this.id= id2; this.name= name2; this.salary= salary2; } private int id; transient private int salary; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getSalary() { return salary; } public void setSalary(int salary) { this.salary = salary; } @Override public String toString() { return "Employee [name=" + name + ", id=" + id + ", salary=" + salary + ", age="+this.age +",address="+this.Address +"]"; } }
Main Class
public class SerializationTest { public static void main(String[] args) { System.out.println("Working Directory = " + System.getProperty("user.dir")); String filename = "Employee.txt"; // make file named "Employee.txt" in the current working directory. Employee e = new Employee("Raj",5,5000); System.out.println(e); // save the object to file FileOutputStream fos = null; ObjectOutputStream out = null; System.out.println("Serialization process begins"); try { fos = new FileOutputStream(filename); out = new ObjectOutputStream(fos); out.writeObject(e); out.close(); } catch (Exception ex) { ex.printStackTrace(); } // read the object from file // save the object to file System.out.println("Deserialization process begins"); FileInputStream fis = null; ObjectInputStream in = null; try { fis = new FileInputStream(filename); in = new ObjectInputStream(fis); e = (Employee) in.readObject(); in.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println(e); } }
Output:
Working Directory = /home/pardeep/NK/Work/Project/JavaTest Employee Class constructor called Employee [name=Raj, id=5, salary=5000, age=5,address=Patna] Serialization process begins Deserialization process begins Employee [name=Raj, id=5, salary=0, age=5,address=Patna]
The readObject method may throw a ClassNotFoundException so we have to insert in try/catch block. JVM have to find the bytecode for the class in order to deserialize an object. If JVM can't find a class during the deserialization of an object the ClassNotFoundException is thrown.