Java Tutorial - Java Script :
Object Output Streams
An object is written to a stream via the ObjectOutputStream class. An object output stream is created with the ObjectOutputStream(OutputStream) constructor. The argument to this constructor can be either of the following:
· An output stream representing the destination where the object should be stored in serial form
· A filter associated with the output stream leading to the destination
As with other streams, you can chain more than one filter between the output stream and the object output stream
The following code creates an output stream and an associated object output stream:FileOutputStream disk = new FileOutputStream(“SavedObject.dat”); ObjectOutputStream disko = new ObjectOutputStream(disk); The object output stream created in this example is called disko. Methods of the disko class can be used to write serializable objects and other information to a file called SavedObject.dat. After you have created an object output stream, you can write an object to it by calling the stream’s writeObject(Object) method. The following statement calls this method on disko, the stream created in the previous example:
disko.writeObject(userData);
This statement writes an object called userData to the disko object output stream. The class represented by userData must be serializable for it to work. An object output stream also can be used to write other types of information with the following methods:
· write(int)—Writes the specified integer to the stream, which should be a value from 0 to 255.
· write(byte[])—Writes the specified byte array.
· write(byte[], int, int)—Writes a subset of the specified byte array. The second argument specifies the first array element to write, and the last argument represents the number of subsequent elements to write.
· writeBoolean(boolean)—Writes the specified boolean.
· writeByte(int)—Writes the specified integer as a byte value.
· writeBytes(String)—Writes the specified string as a series of bytes.
· writeChar(int)—Writes the specified character.
· writeChars(String)—Writes the specified string as a series of characters.
· writeDouble(double)—Writes the specified double.
· writeFloat(float)—Writes the specified float.
· writeInt(int)—Writes the specified int, which unlike the argument to write(int) can be any int value.
· writeLong(long)—Writes the specified long.
· writeShort(short)—Writes the specified short.
The ObjectOutputStream constructor and all methods that write data to an object output stream throw IOException objects. These must be accounted for using a try-catch block or a throws clause. Listing 16.1 contains a Java application that consists of two classes: ObjectWriter and Message. The Message class represents an email message. This class has from and to objects that store the names of the sender and recipient, a now object that holds a Date value representing the time it was sent, and a text array of String objects that holds the message. There also is an int called lineCount that keeps track of the number of lines in the message. When designing a program that transmits and receives email, it makes sense to use some kind of stream to save these messages to disk. The information that constitutes the message must be saved in some form as it is transmitted from one place to another; it also might need to be saved until the recipient is able to read it.
Messages can be preserved by saving each message element separately to a byte or character stream. In the example of the Message class, the from and to objects could be written to a stream as strings, and the text object could be written as an array of strings. The now object is a little trickier because there isn’t a way to write a Date object to a character stream. However, it could be converted into a series of integer values representing each part of a date: hour, minute, second, and so on. Those could be written to the stream. Using an object output stream makes it possible to save Message objects without first translating them into another form. The ObjectWriter class in Listing 16.1 creates a Message object, sets up values for its variables, and saves it to a file called Message.obj via an object output stream.
You should see the following output after you compile and run the ObjectWriter application: Object created successfully.
