Serialization and DeSerialization Example in Java

By |2015-06-15T16:40:50+05:30May 13th, 2015|java|

Serialization

Serialization is the process of storing the state of an object to a sequence of bytes in the secondary storage device. If we want to make a class as Serializable, we should implement the java.io.Serializable interface, It is a marker interface.

SerializationBy knowing the values associated with the properties, we can determine the state of the object. Hence by serializing a class, all the property values can be saved in the files, database or network. If we don’t want to serialize some of the properties, we can make them as transient.

Transient

Transient is the access modifier in Java, When we declare a variable with transient keyword, the value the variable can not be saved.


class TransientDemo implements Serializable{

    transient int a;  // Not Saved

    int b;  // Saved

}

The process of serialization can be done with the help of ObjectOutputStream. The ObjectOutputStream is a class which writes the primitive data types of Java Objects to an OutputStream. To write the primitive to OutputStream, the class should be implements the Serializable interface.

void writeObject(Object obj) is used to write the object to OutputStream.

Example


import java.io.File;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class SerializableDemo implements Serializable {

    public String file_path = "/home/chandrashekhar/Desktop/FileOperations.ser";
    int i = 10;
    transient int j = 20;
    String s = "onlinetutorialspoint";

    public static void main(String[] args) {
        SerializableDemo demo = new SerializableDemo();
        demo.serializeToFS(demo, demo.file_path);

    }

    public void serializeToFS(SerializableDemo demoBean, String path) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(new File(path)));
            oos.writeObject(demoBean);
            oos.flush();
            oos.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            oos = null;
        }
    }
}

After running the above example, the program will create a FileOperations.ser file on your specified path. The sequence of bytes will be stored in the FileOperations.ser.

De serialization

Serialization is also involves the process of get back the state of an object, after reading the sequence of bytes from the stored file. By using the readObject() method available in the ObjectInputStream class, the data will be deserialized.

DeSerialization

Example


import java.io.File;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;

public class DeSerializableDemo implements Serializable {

    public static void main(String[] args) {
        String file_path = "/home/chandrashekhar/Desktop/FileOperations.ser";
        DeSerializableDemo demo = new DeSerializableDemo();
        demo.deserializeFromFS(file_path);

    }

    public void deserializeFromFS(String path) {
        ObjectInputStream ois = null;
        SerializableDemo operations = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(new File(path)));
            operations = (SerializableDemo) ois.readObject();

            System.out.println("Serialization i: " + operations.i);
            System.out.println("Serialization j: " + operations.j);
            System.out.println("Serialization Name: " + operations.s);
            ois.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ois = null;
            operations = null;
        }
    }
}

Output :

Serialization i: 10
Serialization j: 0
Serialization Name: onlinetutorialspoint

On the above out put, the “j” value is “0”. Because “j” is declared as transient, hence the default value is applied to the primitive.

 

You may also Like:

How to writing data to a file in Java ?

How to count number of lines characters and words in a file Java ?

How to Search a file in a Directory using Java ?

About the Author:

Hi Folks, you have reach this so far, that shows you like what you are learning. Then why don't you support us to improve for bettor tutorials by leaving your valuable comments and why not you keep in touch with us for latest updates on your favorite blog @ facebook , twitter , Or Google+ ,

One Comment

  1. chagantirajesh.cultuzz@gmail.com'
    rajesh chaganti May 13, 2015 at 5:40 pm - Reply

    Thanks Chandrashekar….I practised it….it is working awsome….

Leave A Comment