Java ObjectInputStream and DataOutputStream: Stream-Based Serialization and Data Handling

 Java ObjectInputStream and DataOutputStream: Stream-Based Serialization and Data Handling

When working with input and output operations in Java, ObjectInputStream and DataOutputStream are two key classes that extend the functionality of basic streams. These classes are part of the java.io package and are widely used for serialization and for writing/reading primitive data types in a platform-independent way.


1. ObjectInputStream in Java

Purpose

ObjectInputStream is used to deserialize previously serialized Java objects from an input stream. It works in conjunction with ObjectOutputStream, which serializes the object into a stream.

Key Features

  • Reads objects from an input stream.

  • Works with ObjectOutputStream.

  • Supports reading of complex object graphs.

  • The object to be read must implement the Serializable interface.

Constructor

ObjectInputStream(InputStream in) throws IOException

Example: Deserializing an Object

import java.io.*;

class Person implements Serializable {
    String name;
    int age;

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

public class DeserializeDemo {
    public static void main(String[] args) {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("person.ser"))) {
            Person p = (Person) ois.readObject();
            System.out.println("Name: " + p.name);
            System.out.println("Age: " + p.age);
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}

Output

Name: John
Age: 30

2. DataOutputStream in Java

Purpose

DataOutputStream lets you write Java's primitive data types (int, float, boolean, etc.) to an output stream in a portable, machine-independent format.

Key Features

  • Used for writing primitive data types in binary form.

  • Ensures compatibility across platforms.

  • Typically used with DataInputStream.

Constructor

DataOutputStream(OutputStream out)

Example: Writing Primitive Data

import java.io.*;

public class DataOutputExample {
    public static void main(String[] args) {
        try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.bin"))) {
            dos.writeInt(123);
            dos.writeDouble(45.67);
            dos.writeBoolean(true);
            System.out.println("Data written successfully.");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Reading the Data using DataInputStream

import java.io.*;

public class DataInputExample {
    public static void main(String[] args) {
        try (DataInputStream dis = new DataInputStream(new FileInputStream("data.bin"))) {
            int intVal = dis.readInt();
            double doubleVal = dis.readDouble();
            boolean boolVal = dis.readBoolean();
            
            System.out.println("Integer: " + intVal);
            System.out.println("Double: " + doubleVal);
            System.out.println("Boolean: " + boolVal);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Output

Integer: 123
Double: 45.67
Boolean: true

Difference Between ObjectInputStream and DataOutputStream

Feature ObjectInputStream DataOutputStream
Primary Use Deserializing objects Writing primitive data types
Needs Serializable Class Yes No
Read/Write Reads objects only Writes primitive values only
Paired With ObjectOutputStream DataInputStream

Best Practices

  • Always close streams using try-with-resources.

  • Ensure version compatibility of serializable classes.

  • Avoid writing sensitive data without encryption.


Conclusion

ObjectInputStream and DataOutputStream serve different but complementary roles in Java's I/O system. While ObjectInputStream helps in restoring serialized objects, DataOutputStream ensures efficient, portable writing of primitive data. Understanding and using these classes effectively is crucial for handling structured binary data and object persistence in Java applications.


Previous
Next Post »