In this article, we will discuss Java serialization and de-serialization in detail
1. Serialization:
- The process of writing a state of an Object to a file is called Serialization
- In other words, process of saving an Object’s state to a file is called Serialization
- But practically, it is the process of converting and storing Java Object’s state from heap memory (in byte stream) to file supported form (in binary format)
1.1 Demo example on Java Serialization :
- Using ObjectOutputStream and FileOutputStream classes
- Available from java.io package
- We can serialize an Object to file using above classes
Customer class
- Customer class is the one to be serialized
- Therefore, it is must to implement java.io.Serializable interface
- Consists of 3 member variables namely
- Two integer members (customer Id and customer age) and a String member (customer name)
Customer.java
package in.bench.resources.serialization;
import java.io.Serializable;
public class Customer implements Serializable {
// member variables
int customerId;
String customerName;
int customerAge;
// 3-arg parametrized constructor
public Customer(int customerId, String customerName,
int customerAge) {
super();
this.customerId = customerId;
this.customerName = customerName;
this.customerAge = customerAge;
}
// overriding toString() method
@Override
public String toString() {
return "Customer ["
+ "customerId=" + customerId + ","
+ " customerName=" + customerName + ","
+ " customerAge=" + customerAge
+ "]";
}
}
Note: An Object said to be Serializable only if corresponding class implements java.io.Serializable interface
SerializeCustomer.java
package in.bench.resources.serialization;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class SerializeCustomer {
public static void main(String[] args) {
// create a customer object using 3-arg parametrized constructor
Customer customer = new Customer(101, "SJ", 19);
// creating output stream variables
FileOutputStream fos = null;
ObjectOutputStream oos = null;
try {
// for writing or saving binary data
fos = new FileOutputStream("Customer.ser");
// converting java-object to binary-format
oos = new ObjectOutputStream(fos);
// writing or saving customer object's value to stream
oos.writeObject(customer);
oos.flush();
oos.close();
}
catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
}
catch (IOException ioex) {
ioex.printStackTrace();
}
System.out.println("Customer object saved to Customer.ser file");
}
}
Output:
Customer object saved to Customer.ser file
Note: Object must implement java.io.Serializable, otherwise Run time exception will be thrown saying NotSerializableException
2. De-Serialization:
- The process of reading a state of an Object from a file is called De-Serialization
- But practically, it is the process of converting and re-storing Java Object’s state into heap memory from file supported form (which is in binary format)
2.1 Demo example on Java De-Serialization
- Using ObjectInputStream and FileInputStream classes
- Available from java.io package
- We can de-serialize an Object from file using above classes
- Note: we will use same Customer object from above example to de-serialize and also make sure that class implements java.io.Serializable interface
DeSerializeCustomer.java
package in.bench.resources.serialization;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeSerializeCustomer {
public static void main(String[] args) {
// creating input stream variables
FileInputStream fis = null;
ObjectInputStream ois = null;
// creating customer object reference
// to hold values after de-serialization
Customer customer = null;
try {
// reading binary data
fis = new FileInputStream("Customer.ser");
// converting binary-data to java-object
ois = new ObjectInputStream(fis);
// reading object's value and casting to Customer class
customer = (Customer) ois.readObject();
}
catch (FileNotFoundException fnfex) {
fnfex.printStackTrace();
}
catch (IOException ioex) {
ioex.printStackTrace();
}
catch (ClassNotFoundException ccex) {
ccex.printStackTrace();
}
System.out.println("Customer object de-serialized from "
+ "Customer.ser file\nLet's print to console... \n");
// printing customer object to console using toString() method
System.out.println(customer);
}
}
Output:
Customer object de-serialized from Customer.ser file
Let's print to console...
Customer [customerId=101, customerName=SJ, customerAge=19]
Related Articles:
- Java – Serialization and De-Serialization Tutorial Index
- Java – Introduction to Serialization and De-Serialization
- Java – Serializable interface
- Java – Transient keyword with Serialization
- Java – Transient keyword with static variable in Serialization
- Java – Transient keyword with final variable in Serialization
- Java – Serializing a variable with transient modifier or keyword
- Java – Order of Serialization and De-Serialization
- Java – Serialization with Aggregation
- Java – Serialization with Inheritance
- Java – Externalization in detail
- Java – Serializable v/s Externalizable
- Java – Importance of SerialVersionUID in Serialization
- Java – Singleton Design pattern with Serialization
- Java – How to construct a singleton class in a multi-threaded environment ?
- Java – Singleton design pattern, restricting Object creation by overriding readResolve() method
- Java – How to stop Serialization ?
- Java – How to serialize and de-serialize ArrayList ?
- Java – Interview question & answers on Serialization and Externalization
References:
- https://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html
- https://docs.oracle.com/javase/7/docs/platform/serialization/spec/serial-arch.html
- https://docs.oracle.com/javase/7/docs/api/java/io/ObjectOutputStream.html
- https://docs.oracle.com/javase/7/docs/api/java/io/ObjectInputStream.html
- https://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
- https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html
Happy Coding !!
Happy Learning !!