Java IO: ObjectOutputStream
Jakob Jenkov |
The Java ObjectOutputStream
class (java.io.ObjectOutputStream
) enables you to write
Java objects to an OutputStream
instead of just raw bytes.
You wrap an OutputStream
in a ObjectOutputStream
and then you can write objects
to it.
The Java ObjectOutputStream
is often used together with a Java ObjectInputStream
.
The ObjectOutputStream
is used to write the Java objects, and the ObjectInputStream
is
used to read the objects again. You will see an example of this later.
ObjectOutputStream Example
Here is a Java ObjectOutputStream
example:
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("object.data")); MyClass object = new MyClass(); output.writeObject(object); output.close();
First this examples creates a OutputOutputStream
connected to a FileOutputStream
.
Then the example creates a MyClass
object and writes it to the ObjectOutputStream
.
Finally the example closes the 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 ObjectOutputStream With an ObjectInputStream
I promised earlier to show you an example of using the Java ObjectOutputStream
with the
ObjectInputStream
. 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 ObjectOutputStream
When you are finished writing data to the ObjectOutputStream
you should remember to close it.
Closing a ObjectOutputStream
will also close the OutputStream
instance to which the
ObjectOutputStream
is writing.
Closing a ObjectOutputStream
is done by calling its close()
method. Here is how
closing a ObjectOutputStream
looks:
objectOutputStream.close();
You can also use the try-with-resources construct
introduced in Java 7. Here is how to use and close a ObjectOutputStream
looks with the try-with-resources
construct:
OutputStream output = new FileOutputStream("data/data.bin"); try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(output)){ Person person = new Person(); person.name = "Jakob Jenkov"; person.age = 40; objectOutputStream.writeObject(person); }
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 FileOutputStream
instance is not created inside
the try-with-resources block. That means that the try-with-resources block will not automatically close this
FileOutputStream
instance. However, when the ObjectOutputStream
is closed
it will also close the OutputStream
instance it writes to, so the FileOutputStream
instance will get closed when the ObjectOutputStream
is closed.
Tweet | |
Jakob Jenkov |