Byte streams are used for raw binary data, such as images, audio files, and other non-text files.

Common Implementations:

try (FileInputStream in = new FileInputStream("image.jpg");
     FileOutputStream out = new FileOutputStream("copy.jpg")) {
    byte[] buffer = new byte[1024];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1) {
        out.write(buffer, 0, bytesRead);
    }
}

Serialization

Serialization is the process of converting a Java object into a stream of bytes, so that it can be:

The reverse process is called Deserialization—reconstructing the object from the byte stream.

Why Serialization?

Java provides built-in serialization via the java.io.Serializable marker interface.

public class MyClass implements Serializable {
    // no methods to implement — it's a marker interface
}
import java.io.*;

// 1. Create a Serializable class
class Person implements Serializable {
    private String name;
    private int age;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String toString() {
        return name + " (" + age + ")";
    }
}

public class SerializeDemo {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        Person person = new Person("Alice", 30);

        // 2. Serialize object to a file
        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("person.ser"))) {
            out.writeObject(person);
        }

        // 3. Deserialize object from file
        Person deserialized;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("person.ser"))) {
            deserialized = (Person) in.readObject();
        }

        System.out.println("Deserialized: " + deserialized);
    }
}

Output

Deserialized: Alice (30)

Notes

[ O O P b b e j j r e e s c c o t t n O I u n O t p b p u j u t e t S c S t t t r ] r e e a a m m D p e e s r e s r o i n a . l s i e z r e ( f b r i o n m a r f y i l f e i l e )

When Not to Use Serialization

Externalizable

> Externalizable is a Java interface that gives you full control over how an object is serialized and deserialized.

Unlike Serializable (which uses default serialization), Externalizable requires you to explicitly implement how your object’s data is written to and read from a stream.

Interface Definition

public interface Externalizable extends Serializable {
    void writeExternal(ObjectOutput out) throws IOException;
    void readExternal(ObjectInput in) throws IOException, ClassNotFoundException;
}

Key Points

Feature Serializable Externalizable
Control over serialization ❌ No (Java handles it) ✅ Yes (you control everything)
Requires manual implementation ❌ No ✅ Yes (writeExternal, readExternal)
Performance 🟡 Slower due to reflection ✅ Potentially faster
Used for Simplicity Custom format, partial fields, versioning

Example

import java.io.*;

class User implements Externalizable {
    private String name;
    private int age;

    public User() {
        // Required public no-arg constructor
    }

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public void writeExternal(ObjectOutput out) throws IOException {
        out.writeUTF(name);
        out.writeInt(age);
    }

    @Override
    public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
        name = in.readUTF();
        age = in.readInt();
    }

    @Override
    public String toString() {
        return name + " (" + age + ")";
    }
}

public class ExternalizableDemo {
    public static void main(String[] args) throws Exception {
        User user = new User("Sathish", 28);

        try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user.ser"))) {
            out.writeObject(user);
        }

        User restored;
        try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user.ser"))) {
            restored = (User) in.readObject();
        }

        System.out.println("Deserialized: " + restored);
    }
}

Important Notes

[ w [ U r R s i e e t c r e o E n O x s b t u r t j e s e r e r e a u c n r d c t a . E t ] l s x e ( e t d O r e b r U j n s e a e c l r t ( O O O u b b t j j p e e u c c t t t I ] o n u p t u ) t i n )

Use Externalizable When:


Classes
Quiz
Videos
References
Books