Serialization and Deserialization in Java | Code Factory
Reference Link : Link
Donate : Link
Java Serialization allows us to convert Java Object to a Stream of bytes which we can send through a network or save in a 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
The serializable interface in java is a marker interface (method with no body).
Employee.java
package com.codeFactory;import java.io.Serializable;/**
* @author code.factory
*
*/
public class Employee implements Serializable {private int id;
private String name;public Employee(int id, String name) {
this.id = id;
this.name = name;
}public int getId() {
return id;
}public String getName() {
return name;
}@Override
public String toString() {
return "Employee : [id : " + id + ", name : " + name + "]";
}
}
SerializableUtility.java
package com.codeFactory;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;/**
* @author code.factory
*
*/
public class SerializableUtility {public void serialize(Object object, String filePath) {
try {
FileOutputStream fos = new FileOutputStream(filePath);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object);
oos.flush();
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public Object deserialize(String filePath) {
Object object = null;
try {
FileInputStream fis = new FileInputStream(filePath);
ObjectInputStream ois = new ObjectInputStream(fis);
object = ois.readObject();
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
}
Main.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {final private static String filePath = "Test.txt";
public static void main(String... args) {
Employee emp = new Employee(1, "Code Factory");
SerializableUtility su = new SerializableUtility();
su.serialize(emp, filePath);
Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
Output :
Employee : [id : 1, name : Code Factory]
Is sub class Serializable?
If the parent class is Serializable then all the sub class will be Serializable as well.
Person.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Person extends Employee {private String name;public Person(String name) {
super(2, name);
this.name = name;
}public String getName() {
return name;
}}
Main.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {final private static String filePath = "Test.txt";
public static void main(String... args) {
Employee emp = new Employee(1, "Code Factory");
Person person = new Person("Person");
SerializableUtility su = new SerializableUtility();
su.serialize(person, filePath);
Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
Output :
Employee : [id : 2, name : Person]
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.
I have changed code of Employee.java
and Person.java
classes
Employee.java
package com.codeFactory;import java.io.Serializable;/**
* @author code.factory
*
*/
public class Employee implements Serializable {private int id;
private Person person;public Employee(int id, Person person) {
this.id = id;
this.person = person;
}public int getId() {
return id;
}public Person getPerson() {
return person;
}@Override
public String toString() {
return "Employee : [id : " + id + ", person : " + person.getName() + "]";
}
}
Person.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Person {private String name;public Person(String name) {
this.name = name;
}public String getName() {
return name;
}}
Main.java
package com.codeFactory;/**
* @author code.factory
*
*/
public class Main {final private static String filePath = "Test.txt";
public static void main(String... args) {
Employee emp = new Employee(1, new Person("Code Factory"));
SerializableUtility su = new SerializableUtility();
su.serialize(emp, filePath);
Employee employee = (Employee) su.deserialize(filePath);
System.out.println(employee);
}
}
Output :
java.io.NotSerializableException: com.codeFactory.Person
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.codeFactory.SerializableUtility.serialize(SerializableUtility.java:18)
at com.codeFactory.Main.main(Main.java:15)