Serialization in Java

Introduction to Serialization

Serialization is the process of writing the state of an object to a byte stream. It is used when we store the data to a file or a database. Serialization is also used when we implement Remote Method Invocation (RMI).
Serialization in Java is implemented by ObjectInputStream and ObjectOutputStream.
ObjectOutputStream Class

The ObjectOutputStream class extends the OutputStream class and implements the ObjectOutput interface.

Constructor

MethodDescription
void write(byte buffer[])This method is used to write an array of bytes to the invoking stream.
void write(int b)This method used to writes a single byte to the calling stream.
void writeByte(int b)It is used to writes a byte to the invoking stream.
final void writeObject(Object obj)This method used to writes obj to the invoking stream.
void close()It is used to close the invoking stream
void flush()This method used to flushes the output buffers. It cleared the buffers after finalize the output.

Deserialization in Java


Deserialization is used to reconstruct the object from the serialized state. It is used the ObjectInputStream class. Deserialization is the reverse of Serialization.

ObjectInputStream Class

The ObjectInputStream class extends the InputStream class and implements the ObjectInput interface. It is responsible to reading objects from the stream.

Constructor

ObjectInputStream(InputStream inStream) throws IOException

The argument inStream is in the input stream from which serialized object should be read.

Methods

MethodDescription
int read()This method used to returns an integer represent of the next available byte of input.
final Object readObject()It is used to read and returns an object from the invoking stream.
int available()It returns the number of bytes that are available in input buffer.
void close()This method is used to close the invoking stream.

Example: Illustrating the serialization and deserialization in Java

import java.io.*;
public class SerializationDemo
{
     public static void main(String args[])
    {
          //Object Serializable
          try
          {
                TestClass obj1 = new TestClass("Welcome", 101);
                System.out.println("object1: " + obj1);
                FileOutputStream fos = new FileOutputStream("serialized");
                ObjectOutputStream oos = new ObjectOutputStream(fos);
                oos.writeObject(obj1);
                oos.flush();
                oos.close();
           }
           catch(Exception exp)
          {
                exp.printStackTrace();
                System.exit(0);
          }
          //Object Deserializable
         try
         {
              TestClass obj2;
               FileInputStream fis = new FileInputStream("serialized");
               ObjectInputStream ois = new ObjectInputStream(fis);
               obj2 = (TestClass)ois.readObject();
               ois.close();
               System.out.println("object2: " + obj2);
          }
         catch(Exception exp)
         {
               exp.printStackTrace();
               System.exit(0);
         }
     }
}
class TestClass implements Serializable
{
      String s;
      int i;
      public TestClass(String s, int i)
      {
           this.s = s;
           this.i = i;
      }
      public String toString()
      {
          return "String = "+ s + ", int value = "+ i ;
      }
}


Output:
object1: String = Welcome, int value = 101
object2: String = Welcome, int value = 101