Serialization in Java is a mechanism which writing the state of an object into a byte stream so that it can be written into a file and De serialization is the reverse process which rebuilding the object from the serialized state it can be read from the file. Java Serialization API provides the features to perform serialization and De serialization process.

  • A class must implement java.io.Serializable marker interface for serialization of the class.
  • All fields in the class will be serializable only Static field and transient field will not be serializable.

    Example of Serialization and De serialization

    package javatutsworld.com;
    
    public class EmployeeRecord implements java.io.Serializable {
        private int empId;
        private String empName;
        private String empPermanentAddress;
        private transient String empCurrentAddress;
        private transient int empAge;
        private static String empPhone;
    
        public EmployeeRecord(int empId, String empName,
                String empPermanentAddress, String empCurrentAddress, int empAge, String empPhone) {
            super();
            this.empId = empId;
            this.empName = empName;
            this.empPermanentAddress = empPermanentAddress;
            this.empCurrentAddress = empCurrentAddress;
            this.empAge = empAge;
        }
    
        public int getEmpId() {
            return empId;
        }
    
        public void setEmpId(int empId) {
            this.empId = empId;
        }
    
        public String getEmpName() {
            return empName;
        }
    
        public void setEmpName(String empName) {
            this.empName = empName;
        }
    
        public String getEmpPermanentAddress() {
            return empPermanentAddress;
        }
    
        public void setEmpPermanentAddress(String empPermanentAddress) {
            this.empPermanentAddress = empPermanentAddress;
        }
    
        public String getEmpCurrentAddress() {
            return empCurrentAddress;
        }
    
        public void setEmpCurrentAddress(String empCurrentAddress) {
            this.empCurrentAddress = empCurrentAddress;
        }
    
        public int getEmpAge() {
            return empAge;
        }
    
        public void setEmpAge(int empAge) {
            this.empAge = empAge;
        }
    
        public static String getEmpPhone() {
            return empPhone;
        }
    
        public static void setEmpPhone(String empPhone) {
            EmployeeRecord.empPhone = empPhone;
        }
    
    }
    

    In the above class empCurrentAddress, empAge and empPhone are transient and static fields so these fields will not be saved in the file.

    WriteSer.java

    package javatutsworld.com;
    
    import java.io.FileOutputStream;
    import java.io.ObjectOutputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    public class WriteSer {
        public static void main(String args[]) {
            List<EmployeeRecord> listEmp= new ArrayList();
    
            EmployeeRecord empRecord = new EmployeeRecord(001, "Gaurav Pantola",
                    "D 15 Haldwani", "Graeter Noida", 33, "95401112626");
            EmployeeRecord empRecord1 = new EmployeeRecord(002, "Mohd Shariq",
                    "Azad Nagar Roorkee", "Captown 74 Noida", 34, "9540187456");
    
            listEmp.add(empRecord);
            listEmp.add(empRecord1);
            try {
                FileOutputStream fos = new FileOutputStream(
                        "C:\\Temp\\EmpRecord.ser");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(listEmp);
                oos.close();
                fos.close();
                System.out.println("Employee records Serialzation successfully");
            } catch (IOException ioexception) {
                System.out.println(ioexception);
            }
        }
    }
    

    ReadSer.java

    package javatutsworld.com;
    
    import java.io.FileInputStream;
    import java.io.ObjectInputStream;
    import java.io.IOException;
    import java.util.List;
    public class ReadSer {
    
     public static void main(String args[])
     {
        List<EmployeeRecord> empRecord=null;
        try{
          FileInputStream fis = new FileInputStream("C:\\Temp\\EmpRecord.ser");
          ObjectInputStream ois = new ObjectInputStream(fis);
          empRecord = (List<EmployeeRecord>)ois.readObject();
          ois.close();
          fis.close();
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
           return;
        }catch(ClassNotFoundException cnfe)
         {
           System.out.println("EmployeeRecord Class not found.");
           cnfe.printStackTrace();
           return;
         }
        for(EmployeeRecord empR: empRecord){
            System.out.println("Employee Id: " + empR.getEmpId());
            System.out.println("Employee Name: " + empR.getEmpName());
            System.out.println("Employee Permanent Address: " + empR.getEmpPermanentAddress());
            System.out.println("Employee Current Address: " + empR.getEmpCurrentAddress());
            System.out.println("Employee Age: " + empR.getEmpAge());
            System.out.println("Employee Phone: " + empR.getEmpPhone());
          }
    
     }
    }
    

    Output:

    Employee Id: 1
    Employee Name: Gaurav Pantola
    Employee Permanent Address: D 15 Haldwani
    Employee Current Address: null
    Employee Age: 0
    Employee Phone: null
    Employee Id: 2
    Employee Name: Mohd Shariq
    Employee Permanent Address: Azad Nagar Roorkee
    Employee Current Address: null
    Employee Age: 0
    Employee Phone: null
    

    In the above output empCurrentAddress, empAge and empPhone are as 0 and null because these are transient and static fields and are not part of Serialization.