ObjectInputStream

ObjectInputStream is used to deserialize objects—that is, to read objects previously written to a stream using ObjectOutputStream. It reconstructs objects, arrays, and primitive data types from an input stream.

Commonly Used Constructors and Methods

Simple Program – Reading an Object from File

Mahesh wants to read a serialized Employee object from a file named employee.ser that was written by ObjectOutputStream.

import java.io.*;

class Employee implements Serializable {
    int id;
    String name;

    Employee(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

public class SimpleObjectInputStreamExample {
    public static void main(String[] args) {
        // First, write the object to file (for demo purposes)
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee.ser"))) {
            Employee e = new Employee(101, "LotusJavaPrince");
            oos.writeObject(e);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Now, read the object using ObjectInputStream
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("employee.ser"))) {
            Employee emp = (Employee) ois.readObject();
            System.out.println("ID   : " + emp.id);
            System.out.println("Name : " + emp.name);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Output:

ID   : 101
Name : LotusJavaPrince

Problem Statement:

LotusJavaPrince, a banking software developer, stores multiple customer account objects (BankAccount) in a file using ObjectOutputStream. Now Mahesh needs to read all objects from the file using ObjectInputStream for auditing.

Each account has:

  • Account Number (int)
  • Account Holder Name (String)
  • Balance (double)
import java.io.*;
import java.util.*;

class BankAccount implements Serializable {
    int accNo;
    String accHolder;
    double balance;

    BankAccount(int accNo, String accHolder, double balance) {
        this.accNo = accNo;
        this.accHolder = accHolder;
        this.balance = balance;
    }

    void display() {
        System.out.println("Account No  : " + accNo);
        System.out.println("Holder Name : " + accHolder);
        System.out.println("Balance     : " + balance);
        System.out.println("-----------------------------");
    }
}

public class BankAccountReader {
    public static void main(String[] args) {
        // Write multiple accounts (simulate previous storage)
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("accounts.ser"))) {
            oos.writeObject(new BankAccount(1001, "Mahesh", 5000.0));
            oos.writeObject(new BankAccount(1002, "LotusJavaPrince", 12000.75));
            oos.writeObject(new BankAccount(1003, "Kiran", 8700.50));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // Read all objects
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("accounts.ser"))) {
            while (true) {
                try {
                    BankAccount acc = (BankAccount) ois.readObject();
                    acc.display();
                } catch (EOFException eof) {
                    break;  // End of file reached
                }
            }
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Output:

Account No  : 1001
Holder Name : Mahesh
Balance     : 5000.0
-----------------------------
Account No  : 1002
Holder Name : LotusJavaPrince
Balance     : 12000.75
-----------------------------
Account No  : 1003
Holder Name : Kiran
Balance     : 8700.5
-----------------------------Code language: CSS (css)

ObjectInputStream is essential for reading serialized objects from a stream.

  • It works closely with ObjectOutputStream to persist object states.
  • The objects being read must implement Serializable.
  • It simplifies reading complex object structures with just one method call.
Scroll to Top