How to Read an Object from file in Java

By | April 13, 2018

In this java example and tutorial about “How to Read an Object from file in Java”, you will learn how to read an object from the saved file or how to deserialize the serialized file. In the previous java example, you learn about how to write an Object into a file.

The deserialization process is quite similar with the serialization, you need to use ObjectInputStream to read the content of the file and convert it back to a Java object.

FileInputStream fin = new FileInputStream("c:\\temp\\address.ser");
ObjectInputStream ois = new ObjectInputStream(fin);
address = (Address) ois.readObject();

How to Read an Object from file in Java

1. Read Object from File

This class will read a serialized file c:\\temp\\address.ser (created file by this example), and convert it back to a “Address” object and print out the saved value.

ReadObject.java
package com.cimblo.io;

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

public class ReadObject {

	public static void main(String args[]) {
		ReadObject obj = new ReadObject();
		Address address = obj.deserialzeAddress("c:\\temp\\address.ser");
		System.out.println(address);
	}

	public Address deserialzeAddress(String filename) {

		Address address = null;

		FileInputStream fin = null;
		ObjectInputStream ois = null;

		try {
			fin = new FileInputStream(filename);
			ois = new ObjectInputStream(fin);
			address = (Address) ois.readObject();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (fin != null) {
				try {
					fin.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (ois != null) {
				try {
					ois.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return address;
	}

	public Address deserialzeAddressJDK7(String filename) {

		Address address = null;

		try (ObjectInputStream ois 
			= new ObjectInputStream(new FileInputStream(filename))) {
			address = (Address) ois.readObject();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return address;
	}
}

Output

Street : wall street Country : united state

References

  1. ObjectOutputStream JavaDoc
  2. Java Object Serialization

Leave a Reply