Java Serialization allows us to convert Java Object to a Stream of bytes which we can send through a network or save in a flat file or even in a DB for future usage.Deserialization is the process of converting a stream of bytes back to Java Object which can be used in our program. We will be implementing java.io.Serializable interface to achieve serialization
Serializable Interface
The serializable interface in java is a marker interface(method with no body). It adds serialization capabilities to the class Employee. Even though it is a marker interface it must be implemented in the class whose object you want to persist.
import java.io.Serializable; public class Employee implements Serializable { private int empId; private String empName; public int getEmpId() { return empId; } public String getEmpName() { return empName; } public Employee(int empId,String empName) { this.empId=empId; this.empName=empName; } }
Java Serialization Example
We will be using java.util.ObjectOutputStream and java.util.ObjectInputStream to write/read object to/from the file “Persist.txt”
SerializationUtility.java
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; public class SerializationUtility { //Method to serialize and save the object in the file public void serialize(Object obj,String filePath) { try { FileOutputStream fileOutputStream = new FileOutputStream(filePath); ObjectOutputStream outputStream = new ObjectOutputStream(fileOutputStream); outputStream.writeObject(obj); outputStream.flush(); outputStream.close(); } catch(Exception ee) { ee.printStackTrace(); } } //Method to deserialize and return the object public Object deSerialize(String filePath) { Object obj = null; try { FileInputStream fileInputStream = new FileInputStream(filePath); ObjectInputStream inputStream = new ObjectInputStream(fileInputStream); obj = inputStream.readObject(); } catch(Exception ee) { ee.printStackTrace(); } return obj; } }
Our SerializationUtility class has two methods
- serialize() : we will make use of java.util.ObjectOutput stream to write the Object which we pass, to the file “Persist.txt”
- deSerialize(): java.util.ObjectInputStream is used to read the Object from the file and return it back to the user.
Client.java
public class Client { public static void main(String args[]) { //Path to store the Serialized object String filePath="D://Persist.txt"; Employee emp = new Employee(1,"JavaInterviewPoint"); SerializationUtility su = new SerializationUtility(); //Serialize emp object su.serialize(emp, filePath); //De-Serialize Employee object Employee ee = (Employee)su.deSerialize(filePath); System.out.println("Employee id : "+ee.getEmpId()); System.out.println("Employee Name : "+ee.getEmpName()); } }
In our Client.java we have called the serialize() and deSerialize() methods. When we run the above program we will get the output as below and you will have physical file created at your D: Drive
Employee id : 1 Employee Name : JavaInterviewPoint
Is sub class Serializable?
If the parent class is Serializable then all the sub class which extends our parent class will be Serializable as well.
Employee.java
public class Employee implements Serializable { private static final long serialVersionUID = 5414347551602268992L; private int empId; private String empName; public int getEmpId() { return empId; } public String getEmpName() { return empName; } public Employee(int empId,String empName) { this.empId=empId; this.empName=empName; } }
Person.java
public class Person extends Employee{ private String personName; public Person(String personName) { super(2,"Java"); this.personName=personName; } public String getPersonName() { return personName; } }
Here parent class which our Employee.java is Serializable and hence the sub class (Person.java) which extends our parent class is also Serializable.
Other Class Reference in a Serializable class
If we have a non-serializable reference of a class inside a Serializable class, then serialization operation will not be performed.In such case, NonSerializableException will be thrown. Let’s look into the below code.
Employee.java
Employee class is Serializable
public class Employee implements Serializable { private static final long serialVersionUID = 5414347551602268992L; private int empId; private String empName; Location l; public static long getSerialversionuid() { return serialVersionUID; } public Location getL() { return l; } public int getEmpId() { return empId; } public String getEmpName() { return empName; } public Employee(int empId,String empName,Location l) { this.empId=empId; this.empName=empName; this.l = l; l.setAddress("Chennai"); } }
Location.java
Location class is non Serializable
public class Location { private String address; public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Client.java
public class Client { public static void main(String args[]) { //Path to store the Serialized object String filePath="c://Persist.txt"; Employee emp = new Employee(1,"JavaInterviewPoint",new Location()); SerializationUtility su = new SerializationUtility(); //Serialize emp object su.serialize(emp, filePath); //De-Serialize Employee object Employee ee = (Employee)su.deSerialize(filePath); System.out.println("Employee id : "+ee.getEmpId()); System.out.println("Employee Name : "+ee.getEmpName()); System.out.println("Location : "+ee.getL().getAddress()); } }
Now when we run our Client.java we will get java.io.NotSerilizableException
Caused by: java.io.NotSerializableException: com.javainterviewpoint.Location at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.defaultWriteFields(Unknown Source) at java.io.ObjectOutputStream.writeSerialData(Unknown Source) at java.io.ObjectOutputStream.writeOrdinaryObject(Unknown Source) at java.io.ObjectOutputStream.writeObject0(Unknown Source) at java.io.ObjectOutputStream.writeObject(Unknown Source) at com.javainterviewpoint.SerializationUtility.serialize(SerializationUtility.java:17) at com.javainterviewpoint.Client.main(Client.java:14)
As the Location class is not Serializable.
Leave a Reply