Reading and Writing |
Reconstructing an object from a stream requires that the object first be written to a stream. So let's start there.How to Write to an ObjectOutputStream
Writing objects to a stream is a straight-forward process. For example, the following gets the current time in milliseconds by constructing aDate
object and then serializes that object:FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject("Today"); s.writeObject(new Date()); s.flush();ObjectOutputStream
is a processing stream, so it must be constructed on another stream. This code constructs anObjectOutputStream
on aFileOutputStream
, thereby serializing the object to a file namedtheTime
. Next, the stringToday
and aDate
object are written to the stream with thewriteObject
method ofObjectOutputStream
.If an object refers to other objects, then all of the objects that are reachable from the first must be written at the same time so as to maintain the relationships between them. Thus the
writeObject
method serializes the specified object, traverses its references to other objects recursively, and writes them all.
ObjectOutputStream
stream implements theDataOutput
interface that defines many methods for writing primitive data types, such aswriteInt
,writeFloat
, orwriteUTF
. You can use these methods to write primitive data types to anObjectOutputStream
.The
writeObject
method throws aNotSerializableException
if it's given an object that is not serializable. An object is serializable only if its class implements theSerializable
interface.How to Read from an ObjectOutputStream
Once you've written objects and primitive data types to a stream, you'll likely want to read them out again and reconstruct the objects. This is also straight-forward. Here's code that reads in theString
and theDate
object that was written to the file namedtheTime
in the last example:LikeFileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject();ObjectOutputStream
,ObjectInputStream
must be constructed on another stream. In this example, the objects were archived in a file, so the code constructs anObjectInputStream
on aFileInputStream
. Next, the code usesObjectInputStream
'sreadObject
method to read theString
and theDate
objects from the file. The objects must be read from the stream in the same order in which they were written. Note that the return value fromreadObject
is an object that is cast to and assigned to a specific type.The
readObject
method deserializes the next object in the stream and traverses its references to other objects recursively to deserialize all objects that are reachable from it. In this way, it maintains the relationships between the objects.
ObjectInputStream
stream implements theDataInput
interface that defines methods for reading primitive data types. The methods inDataInput
parallel those defined inDataOutput
for writing primitive data types. They include methods such asreadInt
,readFloat
, andreadUTF
. Use these methods to read primitive data types from anObjectInputStream
.
Reading and Writing |