ObjectOutputStream

ObjectOutputStream is used to serialize Java objects—convert them into a byte stream so they can be written to files or sent over a network. It’s the counterpart of ObjectInputStream, which performs deserialization.

Commonly Used Constructors and Methods

The object being written must implement Serializable interface, or NotSerializableException is thrown.

Simple Program – Serializing a Single Object

Mahesh wants to store an Employee object with an ID and name into a file using ObjectOutputStream so that LotusJavaPrince can later retrieve it.

import java.io.*;

class Employee implements Serializable {
    int id;
    String name;

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

public class SimpleObjectOutputStreamExample {
    public static void main(String[] args) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("employee.ser"))) {
            Employee emp = new Employee(101, "LotusJavaPrince");
            oos.writeObject(emp);
            System.out.println("Employee object serialized.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Employee object serialized.
//File employee.ser now contains the binary-serialized form of the object.Code language: JavaScript (javascript)

Problem Statement:

LotusJavaPrince is building a bank record system where customer account objects need to be stored in a file for persistent storage. Each account contains:

  • Account Number (int)
  • Account Holder Name (String)
  • Account Balance (double)

Mahesh helps write multiple such objects to a file using ObjectOutputStream.

import java.io.*;

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;
    }
}

public class BankAccountWriter {
    public static void main(String[] args) {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("accounts.ser"))) {
            // Create and serialize multiple accounts
            BankAccount acc1 = new BankAccount(1001, "Mahesh", 7500.25);
            BankAccount acc2 = new BankAccount(1002, "LotusJavaPrince", 15000.00);
            BankAccount acc3 = new BankAccount(1003, "Paani", 9200.00);

            oos.writeObject(acc1);
            oos.writeObject(acc2);
            oos.writeObject(acc3);

            System.out.println("Multiple bank account objects serialized.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output:

Multiple bank account objects serialized.
// File accounts.ser contains all the serialized BankAccount objects.Code language: JavaScript (javascript)

Behind the Scenes

  • When writeObject() is called, Java performs deep serialization of the object.
  • Fields that are not serializable must be marked transient.
  • Static fields are not serialized as they belong to the class, not the instance.

ObjectOutputStream is ideal for serializing full Java objects to a binary stream.

  • It is commonly used with FileOutputStream, Socket.getOutputStream(), or ByteArrayOutputStream.
  • To deserialize the object, use ObjectInputStream.
Scroll to Top