Java: Externalizable/Custom Serialization and Deserialization Example

The Externalizable interface provides the facility of custom serialization by writing the state of an object into a byte stream by making some changes or compress format.


The Externalizable interface is not a marker interface. It extends Serializable interface and provides two methods for serialization and deserialization of object:

public void writeExternal(ObjectOutput out) throws IOException


public void readExternal(ObjectInput in) throws IOException

Advantage of Externalizable

  • Externalizable is fast in comparison to default serialization. Because JVM uses reflection when you use serializable which is quite slow.
  • Externalizable performance is fast because don’t store unnecessary information. While default serialization store information about class description i.e class description, its superclass, and instance variable associated with that class.

Disadvantage of Externalizable

  • Once any change happens on the class definition, we have to also change our implementation on writeExternal() and readExternal() accordingly.
  • In the case of inheritance, the Subclass object has to coordinate with its superclass to save and store its state by calling super.xxxx() method from subclass.

See Also:

Externalizable Example: Serialization and Deserialization

In this example, Employee class override writeExternal() and readExternal() method for serialization and deserialization of data.  Here we have written our own statement to what data need to serialize. Here you can also consider this example of data compression as storing data as compressed because not storing irrelevant data.


public class Employee implements Externalizable {
	int id;
	String name;
	transient int age;
	public Employee()

	public Employee(int id, String name, int age) { = id; = name;
		this.age = age;

	public void readExternal(ObjectInput oi) throws IOException, ClassNotFoundException {
		String []str=oi.readUTF().split("[|]");[0]);[1];


	public void writeExternal(ObjectOutput oo) throws IOException {




public class ExternalizableCompressionExample {

	public static void main(String[] args) throws Exception {
		// creating employee object
		Employee e1 = new Employee(111, "Saurabh", 35);
		serializeEmployee(e1, "employee.txt");

		e1 = deserializeEmployee("employee.txt");
		System.out.println("Employee Detail :");
		System.out.println("Id :" + + "\nName: " + + "\nAge: " + e1.age);

	private static void serializeEmployee(Employee e1, String fileName) throws Exception {
		// writing object into employee file
		FileOutputStream f = new FileOutputStream(fileName);
		ObjectOutputStream out = new ObjectOutputStream(f);
		// Serialize employee object as stream and write in file
		System.out.println("Employee object Serialize Successfully");

	private static Employee deserializeEmployee(String fileName) throws Exception {
		// Read object stream from file
		ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
		//Deserialize object stream to employee object
		Employee e = (Employee) in.readObject();
		return e;



Employee Detail :
Id :111
Name: Saurabh
Age: 35

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s