Sunday, 14 July 2019

Serialization in Java

It is the process of writing the state of the object to a byte stream. This is useful when you want to save the state of your program to persistent storage area such as a file.At later time you mayrestore these objects by using the process of deserialization.

Objects only that implement the "Serialization interface" can be saved and restored. If we define the object as Transient then we cannot serialize the object.

Recommended learning.

I recommend you to go through the difference between the Transient and  volatile.

Externalizable.

By Default the Serialization happens automatically, when the user wants to have control over this process then there comes this externalizable in picture.

All through the process inStream is the byte stream from which the object is to be read, and
outStream is the byte stream to which the object is to be written

If we want to achieve the serialization below are important.

Object Output.
This is called to serialize the object.

Object Output stream.
This is the class responsible for the writing the objects in to the stream.

Object Input.
This is called to deserialize the Object.

Object Input Stream. 
It is responsible for reading the Objects from a stream.

By Using all of this concepts I have written a small program where we can understand this serialization.

In this  I have created the SerializationExample as Vo class by implementing the Serializable interface .

In the MyClass I am writing this List<SerializationExample> Object to the file and reading from the file.

We can discuss about the complex implementations in the upcoming posts.

package com.searchendeca.sample;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

public class SerializationExample implements Serializable {
transient String s;
int i;
public SerializationExample(String s1,int i1) {
this.s=s1;
this.i=i1;
}
public String toString() {
return "s=" + s + "; i=" + i;
}

}

class MyClass{

public static void main(String args[]) {
//Serialize the object

try {
List<SerializationExample> listObj = new ArrayList<>();
ObjectOutputStream outObj = new ObjectOutputStream(new FileOutputStream("C:\\Users\\Syed\\Sample\\src\\com\\searchendeca\\thread\\sample\\serial.txt"));
SerializationExample se = new SerializationExample("Syed Ghouse", 1);
SerializationExample se1 = new SerializationExample("Syed", 2);
listObj.add(se);
listObj.add(se1);
outObj.writeObject(listObj);
}catch(IOException ie) {
ie.printStackTrace();
}

//Deserialize the object

ObjectInputStream inObj = null;
List<SerializationExample> dse = null;
try {
inObj = new ObjectInputStream(new FileInputStream("C:\\Users\\Syed\\Sample\\src\\com\\searchendeca\\thread\\sample\\serial.txt"));
dse = (List<SerializationExample>) inObj.readObject();
} catch (ClassNotFoundException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dse.stream().forEachOrdered(System.out::println);
}
}

Output:

s=null; i=1
s=null; i=2

No comments:
Write comments