How to read an Object from a File in Java

In an earlier article, we looked at how to write an object to a file using Java. In this short article, you'll learn how to read a Java Object from a file or how to deserialize the serialized object saved in a file.

The deserialization process is quite similar to the serialization process. Basically, to read an object from a file, you need to follow the below steps:

  • Open the file that has the Java Object stored using FileInputStream.
  • Create an instance of ObjectInputStream and pass FileInputStream as an argument to its constructor.
  • Use ObjectInputStream.readObject() method to read the object from the file.
  • The above method will return a generic object of type Object. You need to cast this object to its original type to properly use it.

Create Java Class

Here is how our User.java class looks like that we used to write an object to a file in the previous article:

public class User implements Serializable {

    public String name;
    public String email;
    private String[] roles;
    private boolean admin;

    public User() {
    }

    public User(String name, String email, String[] roles, boolean admin) {
        this.name = name;
        this.email = email;
        this.roles = roles;
        this.admin = admin;
    }

    // getters and setters, toString() .... (omitted for brevity)
}

Read Object from File

The following example shows how you can deserialize the object.dat file and convert it back to a User object in Java 7 or higher:

try (FileInputStream fis = new FileInputStream("object.dat");
     ObjectInputStream ois = new ObjectInputStream(fis)) {

    // read object from file
    User user = (User) ois.readObject();

    // print object
    System.out.println(user);

} catch (IOException | ClassNotFoundException ex) {
    ex.printStackTrace();
}

The above code will print the following on the console:

User{name='John Doe', email='john.doe@example.com', roles=[Member, Admin], admin=true}

If you are using an older Java version (Java 6 or below), you have to manually close ObjectInputStream as shown below:

try {
    FileInputStream fis = new FileInputStream("object.dat");
    ObjectInputStream ois = new ObjectInputStream(fis);

    // read object from file
    User user = (User) ois.readObject();

    // print object
    System.out.println(user);
    
    // close reader
    ois.close();

} catch (IOException | ClassNotFoundException ex) {
    ex.printStackTrace();
}

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.