Core Java - SPLessons

Java Serialization

Chapter 37

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java Serialization

Java Serialization

shape Description

Java Serialization, Serialization is a process of changing the present state of an object in the form of byte streams. After doing Java Serialization process one can read an object from the file, deserialization is used to recreate the exist state of an object. Here one interesting point is that total process is JVM independent .. To serialize or deserialize an object, a developer needs to use two classes as follows, these classes consist the complete methods of Java Serialization and deserialization.

The ObjectOutputStream class will have write methods to serializes an object and sends it to the output stream, ObjectInputStream class also contains the method to deserialize an object as follows.

public final void writeObject(Object x) throws IOException
public final Object readObject() throws IOException, ClassNotFoundException

shape Conceptual
Figure

Serialization

shape Description

Java Serialization, Following is an example to understand the concept of Java Serialization, here first developer is going to create one class with the name Employee as follows.

Employee.java

package serialization;
public class Employee implements java.io.Serializable
{
   public String name;
   public String address;
   public transient int SSN;
   public int number;
   
   public void mailCheck()
   {
      System.out.println("Hello Splesson " + name + " " + address);
   }
}

While working with serialization then the class should implement java.io.Serializable interface. In the following example developer is going to assign the name, address.

SerializeDemo.java

package serialization;
import java.io.*;
public class SerializeDemo
{
   public static void main(String [] args)
   {
      Employee e = new Employee();
      e.name = "SPLESSONS";
      e.address = "Texas, US";
      e.SSN = 123456;
      e.number = 111;
      
      try
      {
         FileOutputStream fileOut =
         new FileOutputStream("E:/SAI/employee.ser");
         ObjectOutputStream out = new ObjectOutputStream(fileOut);
         out.writeObject(e);
         out.close();
         fileOut.close();
         System.out.printf("Serialized data is saved in given folder");
      }catch(IOException i)
      {
          i.printStackTrace();
      }
   }
}

As earlier discussed ObjectOutputStream is used to serialize an object, FileOutputStream is going to write the data to a file.

Output
When compile the code following is the result will be generated.

Serialized data is saved in given folder

Now check the data in the folder, it will be as follows.

¬í sr serialization.Employee™…µ\—c I numberL addresst Ljava/lang/String;L nameq ~ xp ot Texas, USt SPLESSONS

Deserialization

shape Description

In Java Serialization -> deserialization is the process of rebuilding the current state of an object. Based on the above example now the following code is going to deserialize an object.
DeserializeDemo.java

package serialization;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
public class DeserializeDemo
{
   public static void main(String [] args)
   {
      Employee e = null;
      try
      {
         FileInputStream fileIn = new FileInputStream("E:/SAI/employee.ser");
         ObjectInputStream in = new ObjectInputStream(fileIn);
         e = (Employee) in.readObject();
         in.close();
         fileIn.close();
      }catch(IOException i)
      {
         i.printStackTrace();
         return;
      }catch(ClassNotFoundException c)
      {
         System.out.println("Employee class not found");
         c.printStackTrace();
         return;
      }
      System.out.println("Deserialized Employee, Object state is normal now");
      System.out.println("Name: " + e.name);
      System.out.println("Address: " + e.address);
      System.out.println("SSN: " + e.SSN);
      System.out.println("Number: " + e.number);
    }
}

As discussed earlier ObjectInputStream is used to deserialized an object. FileInputStream is used to read the stream of characters.
Output
When compile the code result will be as follows.

Deserialized Employee, Object state is normal now
Name: SPLESSONS
Address: Texas, US
SSN: 0
Number: 111

Summary

shape Key Points

  • The println() is used to print a new line.
  • The printf()is used to format the string.