How to do Serialization and Deserialization ?
n this tutorial, We’ll discuss about java’s java.io.Externalizable interface. The main goal of this interface is to provide custom serialization and deserialization.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
package com.vidvaan.corejava.serilaization01.serilizable; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; public class DeserializationExample { public static void main(String[] args) throws IOException, ClassNotFoundException { // Deserialization Employee deserialisedEmpObj = deserialize(); System.out.println("After deserialization => " + deserialisedEmpObj.toString()); } // Deserialization code private static Employee deserialize() throws IOException, ClassNotFoundException { try (FileInputStream fis = new FileInputStream("Employee.ser"); ObjectInputStream ois = new ObjectInputStream(fis)) { return (Employee) ois.readObject(); } } } |
Before we go ahead, make sure you check out the Java Serialization article.