Serialization and De-Serialization in Java

In this article, we will discuss Java serialization and de-serialization in detail

 

Serialization:

  • The process of writing a state of an Object to a file is called Serialization
  • In other words, the process of saving an Object’s state to a file is called Serialization
  • But practically, it is the process of converting & storing Java Object’s state from heap memory (in byte stream) to file supported form (in binary format)

1-serialization-in-java

 

Demo example on Java Serialization

Using ObjectOutputStream and FileOutputStream classes available from java.io package, we can serialize an Object to file

 

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 member (customer id and customer age) and 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

 

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 & re-storing Java Object’s state into heap memory from file supported form (which is in binary format)

2-deserialization-in-java

 

Demo example on Java De-Serialization

Using ObjectInputStream and FileInputStream classes available from java.io package, we can de-serialize an Object from file

Note: we will use Customer object from above example to de-serialize (and make sure 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]

 

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

 

Read Also:

 

Happy Coding !!
Happy Learning !!