|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Reconstructing an object from a stream requires that the object first be written to a stream. So let's start there.
Writing objects to a stream is a straightforward process. For example, the following gets the current time in milliseconds by constructing aDateobject and then serializes that object:FileOutputStream out = new FileOutputStream("theTime"); ObjectOutputStream s = new ObjectOutputStream(out); s.writeObject("Today"); s.writeObject(new Date()); s.flush();ObjectOutputStreammust be constructed on another stream. This code constructs anObjectOutputStreamon aFileOutputStream, thereby serializing the object to a file namedtheTime. Next, the stringTodayand aDateobject are written to the stream with thewriteObjectmethod ofObjectOutputStream.Thus, the
writeObjectmethod serializes the specified object, traverses its references to other objects recursively, and writes them all. In this way, relationships between objects are maintained.
ObjectOutputStreamimplements theDataOutputinterface 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
writeObjectmethod throws aNotSerializableExceptionif it's given an object that is not serializable. An object is serializable only if its class implements theSerializableinterface.
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 straightforward. Here's code that reads in theStringand theDateobjects that were written to the file namedtheTimein the previous example:LikeFileInputStream in = new FileInputStream("theTime"); ObjectInputStream s = new ObjectInputStream(in); String today = (String)s.readObject(); Date date = (Date)s.readObject();ObjectOutputStream,ObjectInputStreammust be constructed on another stream. In this example, the objects were archived in a file, so the code constructs anObjectInputStreamon aFileInputStream. Next, the code usesObjectInputStream'sreadObjectmethod to read theStringand theDateobjects 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 fromreadObjectis an object that is cast to and assigned to a specific type.The
readObjectmethod 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.
ObjectInputStreamstream implements theDataInputinterface that defines methods for reading primitive data types. The methods inDataInputparallel those defined inDataOutputfor writing primitive data types. They include methods such asreadInt,readFloat, andreadUTF. Use these methods to read primitive data types from anObjectInputStream.
|
|
Start of Tutorial > Start of Trail > Start of Lesson |
Search
Feedback Form |
Copyright 1995-2004 Sun Microsystems, Inc. All rights reserved.