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: ...
Get Java I/O now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.