Objects are serialized by object output streams. They are deserialized by object input streams. These classes 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 adds methods to write objects. The ObjectInput interface is a subinterface of java.io.DataInput that adds methods to read objects. By extending DataInput and DataOutput, ObjectInput and ObjectOutput guarantee that their implementers are able to read and write primitive types like int and double, as well as objects. Since an object may contain fields of primitive types, anything that has to read or write the state of an object also has to be able to read or write the primitive fields the object contains.
java.io.ObjectStreamConstants is an unimportant interface that merely declares mnemonic constants for "magic numbers" used in the object serialization file format. A major goal of the object stream classes is shielding client programmers from these sorts of details about the format used to serialize objects.
ObjectOutputStream and ObjectInputStream are filter streams, and thus they are chained to underlying streams in their constructors:
public ObjectOutputStream(OutputStream out) throws IOException public ObjectInputStream(InputStream in) throws IOException
To write an object onto a stream, pass the object to the ObjectOutputStream's writeObject( ) method:
public final void writeObject(Object o) throws IOException
For example, this code fragment chains an ObjectOutputStream to a FileOutputStream and writes a java.awt.Point object into that file:
Point p = new Point(34, 22); FileOutputStream fout = new FileOutputStream("point.ser"); ObjectOutputStream oout = new ObjectOutputStream(fout); oout.writeObject(p); oout.close( );
Later, the object can be read back using the readObject( ) method of the ObjectInputStream class:
public final Object readObject( ) throws OptionalDataException, ClassNotFoundException, IOException
For example:
FileInputStream fin = new FileInputStream("point.ser"); ObjectInputStream oin = new ObjectInputStream(fin); Object o = oin.readObject( ); Point p = (Point) o; oin.close( );
The reconstituted Point has the same values as the original Point. Its x is 34 and its y is 22, just like the Point object that was written. However, since readObject( ) is only declared to return an Object, you usually need to cast the deserialized object to a more specific type.
Both writeObject( ) and readObject( ) tHRow IOException for all the usual reasons an I/O operation can fail (disk filling up, network connection being severed, etc.). There are also several object-serialization specific subclasses of IOException. For example, an InvalidClassException indicates that the data in the stream can't be matched to the corresponding class (for instance, because the version of the class that was serialized is not the same as the version of the class used in the VM deserializing the object). The readObject( ) method also throws a ClassNotFoundException if a definition for the class of the object read from the input stream is not available in the current VM.
Basic I/O
Introducing I/O
Output Streams
Input Streams
Data Sources
File Streams
Network Streams
Filter Streams
Filter Streams
Print Streams
Data Streams
Streams in Memory
Compressing Streams
JAR Archives
Cryptographic Streams
Object Serialization
New I/O
Buffers
Channels
Nonblocking I/O
The File System
Working with Files
File Dialogs and Choosers
Text
Character Sets and Unicode
Readers and Writers
Formatted I/O with java.text
Devices
The Java Communications API
USB
The J2ME Generic Connection Framework
Bluetooth
Character Sets