O'Reilly logo

Java I/O by Elliotte Rusty Harold

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Object Streams

Objects are serialized by object output streams. They are deserialized by object input streams. These are instances of java.io.ObjectOutputStream and java.io.ObjectInputStream, respectively:

public class ObjectOutputStream extends OutputStream 
  implements ObjectOutput, ObjectStreamConstants 
public class ObjectInputStream extends InputStream 
  implements ObjectInput, ObjectStreamConstants

The ObjectOutput interface is a subinterface of java.io.DataOutput that declares the basic methods used to write objects and data. The ObjectInput interface is a subinterface of java.io.DataInput that declares the basic methods used to read objects and data. java.io.ObjectStreamConstants is an unimportant interface that merely declares mnemonic constants for “magic numbers” used in the object serialization. (A major goal of the object stream classes is shielding client programmers from details of the format used to serialize objects such as magic numbers.)

Although these classes are not technically filter output streams, since they do not extend FilterOutputStream and FilterInputStream, they are chained to underlying streams in the constructors:

public ObjectOutputStream(OutputStream out) throws IOException
public ObjectInputStream(InputStream in) throws IOException

To write an object onto a stream, you chain an object output stream to the stream, then pass the object to the object output stream’s writeObject() method:

public final void writeObject(Object o) throws IOException

For example: ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required