Java-Object Serialization

Object Serialization

We have seen in previous program’s how we can read and write characters, bytes, and primitive data types. It is also possible to perform input and output operations on objects using the object streams. The object streams are created using the ObjectlnputStream and ObjectOutputStream classes. In this case, we may declare records as objects and use the object stream classes to write and read these objects from files. This process is known as object serialization. This is useful when you want to save the state of your program to a persistent storage area, such as a file. At a later time, you may restore these objects by using the process of deserialization.

Serializable Interface

Only an object that implements the Serializable interface can be saved and restored by the serialization facilities. The Serializable interface defines no members. It is simply used to indicate that a class may be serialized. If a class is serializable, all of its subclasses are also serializable.

The following program illustrates how to use object serialization and deserialization. It begins by instantiating an object of class Data. This object has two instance variables that are of type int. This is the information we want to save and restore. A FileOutputStream is created that refers to a file named “obj.da,” and an ObjectOutputStream is created for that file stream. The writeObject( ) method of ObjectOutputStream is then used to serialize our objects. The object output stream is and closed. A FilelnputStream is then created that refers to the file named “obj.da,” and an ObjectInputStream is created for that file stream. The readObject( ) method of ObjectInputStream is then used to deserialize our object. The object input stream is then closed.

Program

sdsd

Program Source

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.io.IOException;

class Data implements Serializable {
    
    int data1;
    int data2;
    
    Data(int da1,int da2)
    {
        data1 = da1;
        data2 = da2;
    }
    
    void printData()
    {
        System.out.println("Data1 = "+data1);
        System.out.println("Data2 = "+data2);
    }
}

class Javaapp {
    
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        
        FileOutputStream fos = new FileOutputStream("obj.da");
        ObjectOutputStream objs = new ObjectOutputStream(fos);
        Data d1 = new Data(20,40);
        Data d2 = new Data(60,80);
        objs.writeObject(d1);
        objs.writeObject(d2);
        objs.close();
        fos.close();
        
        FileInputStream fis = new FileInputStream("obj.da");
        ObjectInputStream obji = new ObjectInputStream(fis);
        Data dobj1 = (Data)obji.readObject();
        dobj1.printData();
        Data dobj2 = (Data)obji.readObject();
        dobj2.printData();
        obji.close();
        fis.close();
    }
}

Leave a Comment