Java IO: ObjectInputStream
Jakob Jenkov |
The Java ObjectInputStream class (java.io.ObjectInputStream) enables you to read Java
objects from an InputStream instead of just raw bytes.
You wrap an InputStream in a ObjectInputStream and then you can read objects
from it. Of course the bytes read must represent
a valid, serialized Java object. Otherwise reading objects will fail.
Normally you will use the ObjectInputStream to read objects written (serialized) by a
Java ObjectOutputStream . You will see an example of that later.
ObjectInputStream Example
Here is a Java ObjectInputStream example:
ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream("object.data"));
MyClass object = (MyClass) objectInputStream.readObject();
//etc.
objectInputStream.close();
For this ObjectInputStream example to work the object you read must be an instance of MyClass, and must
have been serialized into the file "object.data" via an ObjectOutputStream.
Before you can serialize and de-serialize objects the class of the object must implement
java.io.Serializable. For more info, see Java Serializable.
Using an ObjectInputStream With an ObjectOutputStream
I promised earlier to show you an example of using the Java ObjectInputStream with the
ObjectOutputStream. Here is that example:
import java.io.*;
public class ObjectInputStreamExample {
public static class Person implements Serializable {
public String name = null;
public int age = 0;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream objectOutputStream =
new ObjectOutputStream(new FileOutputStream("data/person.bin"));
Person person = new Person();
person.name = "Jakob Jenkov";
person.age = 40;
objectOutputStream.writeObject(person);
objectOutputStream.close();
ObjectInputStream objectInputStream =
new ObjectInputStream(new FileInputStream("data/person.bin"));
Person personRead = (Person) objectInputStream.readObject();
objectInputStream.close();
System.out.println(personRead.name);
System.out.println(personRead.age);
}
}
This example first creates an ObjectOutputStream connected to a FileOutputStream.
Then it creates a Person object and writes it to the ObjectOutputStream, and then
closes the ObjectOutputStream.
Then the example creates an ObjectInputStream connected to the same file the ObjectOutputStream
was connected to. The example then reads in an object from the ObjectInputStream and casts it to a
Person object. After that the ObjectInputStream is also closed, and the values read into
the Person object are printed to System.out.
The output printed from running this example should be:
Jakob Jenkov 40
Closing a ObjectInputStream
When you are finished reading data from the ObjectInputStream you should remember to close it.
Closing a ObjectInputStream will also close the InputStream instance from which the
ObjectInputStream is reading.
Closing a ObjectInputStream is done by calling its close() method. Here is how
closing a ObjectInputStream looks:
objectInputStream.close();
You can also use the try-with-resources construct
introduced in Java 7. Here is how to use and close a ObjectInputStream looks with the try-with-resources
construct:
InputStream input = new FileInputStream("data/data.bin");
try(ObjectInputStream objectInputStream =
new ObjectInputStream(input)){
Person personRead = (Person) objectInputStream.readObject();
}
Notice how there is no longer any explicit close() method call. The try-with-resources construct
takes care of that.
Notice also that the first FileInputStream instance is not created inside
the try-with-resources block. That means that the try-with-resources block will not automatically close this
FileInputStream instance. However, when the ObjectInputStream is closed
it will also close the InputStream instance it reads from, so the FileInputStream
instance will get closed when the ObjectInputStream is closed.
| Tweet | |
Jakob Jenkov | |











