The Java I/O library provides classes that allow us to read or write a user-defined object from or to a stream.

The two classes:

  •  ObjectInputStream 
  •  ObjectOutputStream

provide this functionality. Java also defines an interface called Serializable. If we want to read/write objects, the class representing the desired object must implement this interface. The Serializable interface does not have any methods, so its implementation remains null.

Let’s illustrate the use of these classes with an example. Suppose we have created a Student class to hold a student’s ID as well as the student’s first and the last names. We now want to create a student database holding the data for the various students in the class. Our program should also be able to retrieve this data. The program shown here does just this.

Program Code                                     
public class ObjectSerializationApp {
                     public static void main(String[] args) {
                             ObjectOutputStream objectWriter = null;
                             ObjectInputStream objectReader = null;
                             try {
                                     objectWriter = new ObjectOutputStream(new FileOutputStream(                                                                                      "student.dat"));                                     objectWriter.writeObject(new Student(1, "Jack", "Mathews"));                                     objectWriter.writeObject(new Student(2, "Eric", "Campbell"));                                     objectWriter.writeObject(new Student(3, "Johanna", "Wick"));  
                                                System.out.println("Printing list of students in the database:");                                       objectReader = new ObjectInputStream(new FileInputStream(                                                           "student.dat"));
                                                   for (int i = 0; i < 3; i++) {
                                                System.out.println(objectReader.readObject());                                       }
                             } catch (Exception e) {
                             } finally {
                                      try {
                                      } catch (IOException ie) {
class Student implements Serializable {
                     private String firstName;
                     private String lastName;
                     private int id;
                     public Student(int id, String firstName, String lastName) {
                    = id;
                             this.firstName = firstName;
                             this.lastName = lastName;
                     public String toString() {
                             return ("ID:" + id + " " + firstName + " " + lastName);                      }

·     Let’s first discuss the Student class. The class implements the Serializable interface:

       class Student implements Serializable {

·     As mentioned earlier, the Serializable interface does not have any methods to implement and is therefore known as a marker interface. The class constructor takes three arguments and stores their values in instance variables. The Student class overrides the toString method to provide its own string representation.

·     The main method of the ObjectSerializationApp class constructs an instance of ObjectOutputStream, as follows:

              objectWriter = new ObjectOutputStream(new FileOutputStream("student.dat"));

·         Note that this statement creates a new file called student.dat. If the file with this name already exists, it will overwrite its contents.

Once an instance of the ObjectOutputStream is obtained, we can call its writeObject method to write any serializable object to it. The program constructs and writes three student objects to the student.dat file using the following statements:

·         objectWriter.writeObject(new Student(1, "Jack", "Mathews"));

·         objectWriter.writeObject(new Student(2, "Eric", "Campbell"));

·         objectWriter.writeObject(new Student(3, "Johanna", "Wick"));


After writing objects, the output file is closed via a call to its close method. The program then reopens the student.dat file by constructing an ObjectInputStream object around it:

objectReader = new ObjectInputStream(new FileInputStream("student.dat"));

The program then reads the three objects from this file and prints their string representation using the following for loop:

for (int i = 0; i < 3; i++) {


 The method readObject returns a Student object, which is printed to the console by calling the implicitly overridden toString method.


When we run the program, we’ll see the following output on our console:

C:\360\io>java ObjectDemo
Printing list of students in the database:
ID:1 Jack Mathews
ID:2 Eric Campbell
ID:3 Johanna Wick


We can also check for the presence of the newly created student.dat file in our current folder.

  Modified On Mar-15-2018 03:27:39 AM

Leave Comment