Serialization and Deserialization in Java
In java, the Serialization is the process of converting an object into a byte stream so that it can be stored on to a file, or memory, or a database for future access. The deserialization is reverse of serialization. The deserialization is the process of reconstructing the object from the serialized state.
Using serialization and deserialization, we can transfer the Object Code from one Java Virtual machine to another.
Serialization in Java
In a java programming language, the Serialization is achieved with the help of interface Serializable. The class whose object needs to be serialized must implement the Serializable interface.
We use the ObjectOutputStream class to write a serialized object to write to a destination. The ObjectOutputStream class provides a method writeObject() to serializing an object.
We use the following steps to serialize an object.
- Step 1 - Define the class whose object needs to be serialized; it must implement Serializable interface.
- Step 2 - Create a file reference with file path using FileOutputStream class.
- Step 3 - Create reference to ObjectOutputStream object with file reference.
- Step 4 - Use writeObject(object) method by passing the object that wants to be serialized.
- Step 5 - Close the FileOutputStream and ObjectOutputStream.
Let's look at the following example program for serializing an object.
import java.io.*;
public class SerializationExample {
public static void main(String[] args) {
Student stud = new Student();
stud.studName = "Rama";
stud.studBranch = "IT";
try {
FileOutputStream fos = new FileOutputStream("my_data.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(stud);
oos.close();
fos.close();
System.out.println("The object has been saved to my_data file!");
}
catch(Exception e) {
System.out.println(e);
}
}
}
When we run the above program, it produce the following output.
Deserialization in Java
In a java programming language, the Deserialization is achieved with the help of class ObjectInputStream. This class provides a method readObject() to deserializing an object.
We use the following steps to serialize an object.
- Step 1 - Create a file reference with file path in which serialized object is available using FileInputStream class.
- Step 2 - Create reference to ObjectInputStream object with file reference.
- Step 3 - Use readObject() method to access serialized object, and typecaste it to destination type.
- Step 4 - Close the FileInputStream and ObjectInputStream.
Let's look at the following example program for deserializing an object.
import java.io.*;
public class DeserializationExample {
public static void main(String[] args) throws Exception{
try {
FileInputStream fis = new FileInputStream("my_data.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Student stud2 = (Student) ois.readObject();
System.out.println("The object has been deserialized.");
fis.close();
ois.close();
System.out.println("Name = " + stud2.studName);
System.out.println("Department = " + stud2.studBranch);
}
catch(Exception e) {
System.out.println(e);
}
}
}
When we run the above program, it produce the following output.