C

Core Java tutorial for beginners

Clean • Professional

Object Streams in Java (ObjectInputStream & ObjectOutputStream) Explained

2 minute

Object Streams in Java

Java provides Object Streams to perform object-level I/O , meaning you can read/write entire Java objects to files, memory, or network streams. This is the foundation of Serialization & Deserialization.

What are Object Streams?

Object Streams are specialized streams that allow Java objects to be written as bytes and restored back as full objects.

Java provides two main classes:

learn code with durgesh images

  • ObjectOutputStream → converts an object into a byte stream and writes it to a file.
  • ObjectInputStream → reads the byte stream and converts it back into the original object.

These classes are part of the java.io package.

Requirements for Object Streams

To serialize an object:

  • The class must implement Serializable
  • All non-transient fields must be serializable
  • serialVersionUID is recommended
  • Nested objects should also be serializable

ObjectOutputStream (Writing Objects to File)

ObjectOutputStream is used to serialize Java objects and write them into a binary file.

  • Writes objects using writeObject().
  • Works only with classes that implement Serializable.
  • Stores complete object state (fields).

ObjectInputStream (Reading Objects from File)

ObjectInputStream is used to deserialize objects from a file and reconstruct them in memory.

  • Reads objects using readObject().
  • Must read in the same order they were written.
  • Class definition must be available at runtime.

Complete Example: Serialization + Deserialization

Below is the full demo with both ObjectOutputStream & ObjectInputStream.

Step 1: Create a Serializable Class

import java.io.Serializable;

public class Student implements Serializable {
    private int id;
    private String name;

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

    public String toString() {
        return "Student{id=" + id + ", name='" + name + "'}";
    }
}

Step 2: Write Object Using ObjectOutputStream (Serialization)

import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

public class WriteObject {
    public static void main(String[] args) {
        try (ObjectOutputStream oos =
                new ObjectOutputStream(new FileOutputStream("student.ser"))) {

            Student s1 = new Student(101, "Amit");
            oos.writeObject(s1);

            System.out.println("Object written successfully!");

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

Step 3: Read Object Using ObjectInputStream (Deserialization)

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class ReadObject {
    public static void main(String[] args) {
        try (ObjectInputStream ois =
                new ObjectInputStream(new FileInputStream("student.ser"))) {

            Student s = (Student) ois.readObject();
            System.out.println("Read Object: " + s);

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

How Serialization Works Internally

  • JVM converts object → byte stream.
  • Writes object metadata, class name, field values.
  • Deserialization reconstructs the object.
  • Requires matching serialVersionUID for version control.

Important Notes

Class must implement Serializable

class Student implements Serializable { }

Static & transient fields are not serialized

Order of readObject() must match writeObject()

serialVersionUID prevents compatibility issues


Multiple Objects with Object Streams

Yes, you can write multiple objects:

oos.writeObject(obj1);
oos.writeObject(obj2);

And read:

obj1 = (MyClass) ois.readObject();
obj2 = (MyClass) ois.readObject();

Points to Remember

  • ObjectOutputStreamwriteObject() → saves objects.
  • ObjectInputStreamreadObject() → loads objects.
  • Used for Serialization / Deserialization.
  • Only works with Serializable classes.
  • Stores full object state in a .ser file.

Article 0 of 0