Object Serialization transform an object into a sequence of bytes so it cam be written to disc or can be transfer to the server. This Sequence of bytes can be later deserialized into an original object. After deserialization, the object has the same state as it had when it was serialized

Java provides this facility through ObjectInput and ObjectOutput interfaces. The concrete implementation of ObjectOutput and OnjectInput interfaces is provided in ObjectOutputStream and ObjectInputStream classes respectively. These two interfaces have the following methods:

  final void writeObject(Object obj) tthrows IOException
   final Object readObejct() through IOException, ClassNotFoundException

The writeObject() method can be used be used to write any object to a stream, including strings and arrays and readObject() method can be used to read any stream as long as an object supports java.io.Serializable interface.

This code first writes the serializable objects to a file. And then read the same file and prints the object on the console. While writing to the file the objects are converted to the byte streams and while reading this byte stream is de serialized to get the objects in the original state.

 import java.io.*;

public class SerializableEmployee implements Serializable {
    String name;
    int age;
    double salary;

    SerializableEmployee(String name, int age, double salary) {
        this.name = name;
        this.age = age;
        this.salary = salary;
    }

    public void showDetails() {
        System.out.println("Name   : " + name);
        System.out.println("Age    : " + age);
        System.out.println("Salary : " + salary);
    }
}

class ObjectSerializationDemo {
    void writeData() {
        SerializableEmployee db[] = {
                new SerializableEmployee("Tom", 25, 1200.25),
                new SerializableEmployee("Dick", 22, 1223.25),
                new SerializableEmployee("Harry", 25, 1345.25) };
        try {
            FileOutputStream out = new FileOutputStream("c://employee.txt");
            try {
                ObjectOutputStream sout = new ObjectOutputStream(out);
                for (int i = 0; i < db.length; i++) {
                    sout.writeObject(db[i]);
                }
                sout.close();

            } catch (Exception e) {
                        e.printStackTrace();
        }
    }
     void readData() {
        try {
            FileInputStream in = new FileInputStream("c://employee.txt");
            try {
                ObjectInputStream sin = new ObjectInputStream(in);
                try {
                    SerializableEmployee se = (SerializableEmployee) sin
                            .readObject();
                    se.showDetails();
                    se = (SerializableEmployee) sin.readObject();
                    se.showDetails();
                    se = (SerializableEmployee) sin.readObject();
                    se.showDetails();

                    sin.close();

                } catch (Exception e) {
                        e.printStackTrace();
                }
        }

    public static void main(String[] args) {
        ObjectSerializationDemo impl = new ObjectSerializationDemo();
        impl.writeData();
        impl.readData();

    }

}

Output:

Name   : Tom
Age    : 25
Salary : 1200.25
Name   : Dick
Age    : 22
Salary : 1223.25
Name   : Harry
Age    : 25
Salary: 1345.25