An object is read from a stream using the ObjectInputStream class. As with other streams, working with an object input stream is similar to working with an object output stream. The primary difference is the change in the data’s direction. An object input stream is created with the ObjectInputStream(InputStream)constructor. Two exceptions are thrown by this constructor: IOException and StreamCorruptionException. IOException, common to stream classes, occurs whenever any kind of input/output error occurs during the data transfer. StreamCorruptionException is specific to object streams, and it indicates that the data in the stream is not a serialized object. An object input stream can be constructed from an input stream or a filtered stream. The following code creates an input stream and an object input stream to go along with it:
try {
FileInputStream disk = new FileInputStream(
“SavedObject.dat”);
ObjectInputStream obj = new ObjectInputStream(disk);
} catch (IOException ie) {
System.out.println(“IO error –- “ + ie.toString());
} catch (StreamCorruptionException se) {
System.out.println(“Error – data not an object.”);
}
This object input stream is set up to read from an object stored in a file called SavedObject. dat. If the file does not exist or cannot be read from disk for some reason, an IOException is thrown. If the file isn’t a serialized object, a thrown StreamCorruptionException indicates this problem. An object can be read from an object input stream by using the readObject() method, which returns an Object. This object can be immediately cast into the class to which it belongs, as in the following example:
WorkData dd = (WorkData)disk.readObject();
This statement reads an object from the disk object stream and casts it into an object of the class WorkData. In addition to IOException, this method throws OptionalDataException and ClassNotFoundException errors. OptionalDataException indicates that the stream contains data other than serialized object data, which makes it impossible to read an object from the stream. ClassNotFoundException occurs when the object retrieved from the stream belongs to a class that could not be found. When objects are serialized, the class is not saved to the stream. Instead, the name of the class is saved to the stream, and the class is loaded by the Java interpreter when the object is loaded from a stream. Other types of information can be read from an object input stream with the following methods:
read()—Reads the next byte from the stream, which is returned as an int.
· read(byte[], int, int)—Reads bytes into the specified byte array. The second argument specifies the first array element where a byte should be stored. The last argument represents the number of subsequent elements to read and store in the array.
· readBoolean()—Reads a boolean value from the stream.
· readByte()—Reads a byte value from the stream.
· readChar()—Reads a char value from the stream.
· readDouble()—Reads a double value from the stream.
· readFloat()—Reads a float value from the stream.
· readInt()—Reads an int value from the stream.
· readLine()—Reads a String from the stream.
· readLong()—Reads a long value from the stream.
· readShort()—Reads a short value from the stream.
· readUnsignedByte()—Reads an unsigned byte value and returns it as an int.
· readUnsignedShort()—Reads an unsigned short value and returns it as an int.
Each of these methods throws an IOException if an input/output error occurs as the stream is being read. When an object is created by reading an object stream, it is created entirely from the variable and object information stored in that stream. No constructor method is called to create variables and set them up with initial values. There’s no difference between this object and the one originally serialized.
The output of this program is as follows:
Message:
From: Sam Wainwright, London
To: George Bailey, Bedford Falls Date: Sat Jan 13 20:53:40 EST 2007 Mr. Gower cabled you need cash. Stop. My office instructed to advance you up to twenty-five thousand dollars. Stop. Hee-haw and Merry Christmas.