writing-and-reading-data-in-file-using-objectoutputstream-and-objectInputstream-in-java-with-examples

admin

2/27/2025

Java file operations objectoutputstream vs objectInputstream

Go Back

Writing and Reading Data in File Using ObjectInputStream and ObjectOutputStream in Java with Examples

Updated: 06/Feb/2025 by Computer Hope

File handling in Java is a crucial aspect of programming, especially when working with object serialization. In this tutorial, we will explore how to write and read data in a file using ObjectOutputStream and ObjectInputStream in Java.

Java file operations objectoutputstream vs objectInputstream

What is Object Serialization?

Object serialization in Java is the process of converting an object into a byte stream, which can then be persisted into a file or transferred over a network. Deserialization is the reverse process where the byte stream is converted back into an object.

Example Code for Writing and Reading Objects in Java

Below is a Java program demonstrating how to use ObjectOutputStream for writing objects into a file and ObjectInputStream for reading those objects back from the file.

Java Program: Writing and Reading Objects from a File

import java.io.*;
import java.util.*;

class Emp implements Serializable {
    private int age;
    private String name;
    private double sal;
    
    public void get() {
        Scanner kb = new Scanner(System.in);
        System.out.println("Enter age, name, and salary:");
        age = kb.nextInt();
        name = kb.next();
        sal = kb.nextDouble();
    }
    
    public void show() {
        System.out.println("Age: " + age);
        System.out.println("Name: " + name);
        System.out.println("Salary: " + sal);
    }
}

public class FileExample6 {
    public static void main(String args[]) {
        try {
            // Writing object to file
            ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:/emp.dat"));
            Emp E = new Emp();
            E.get();
            oos.writeObject(E);
            oos.close();
            
            // Reading object from file
            ObjectInputStream ios = new ObjectInputStream(new FileInputStream("D:/emp.dat"));
            Emp F = (Emp) ios.readObject();
            F.show();
            ios.close();
        } catch (Exception ex) {
            System.out.println("Error in writing or reading Object: " + ex.getMessage());
        }
    }
}

Explanation of the Code

  1. Defining the Class:

    • The Emp class implements Serializable to allow object serialization.
    • The class contains three attributes: age, name, and sal (salary).
    • The get() method takes input for these attributes, and show() displays them.
  2. Writing an Object to a File:

    • The ObjectOutputStream is used to serialize the object and save it in a file.
    • The writeObject(E) method writes the Emp object to the file.
  3. Reading an Object from a File:

    • The ObjectInputStream is used to read the serialized object.
    • The readObject() method retrieves the object from the file and casts it back to the Emp class.
  4. Handling Exceptions:

    • The try-catch block ensures that errors such as IOException and ClassNotFoundException are properly handled.

Key Takeaways

  • ObjectOutputStream and ObjectInputStream are used to write and read objects in Java.
  • The Serializable interface is mandatory for object serialization.
  • Proper exception handling should be in place to avoid runtime errors.

Conclusion

Using ObjectOutputStream and ObjectInputStream in Java makes object storage and retrieval efficient and easy. This method is useful for saving user data, game progress, and other stateful information.

Table of content