Java Reference
In-Depth Information
LISTING 16.1
Continued
14: try {
15: FileOutputStream fo = new FileOutputStream(
16: “Message.obj”);
17: ObjectOutputStream oo = new ObjectOutputStream(fo);
18: oo.writeObject(mess);
19: oo.close();
20: System.out.println(“Object created successfully.”);
21: } catch (IOException e) {
22: System.out.println(“Error — “ + e.toString());
23: }
24: }
25: }
26:
27: class Message implements Serializable {
28: int lineCount;
29: String from, to;
30: Date when;
31: String[] text;
32:
33: void writeMessage(String inFrom,
34: String inTo,
35: Date inWhen,
36: String[] inText) {
37:
38: text = new String[inText.length];
39: for (int i = 0; i < inText.length; i++)
40: text[i] = inText[i];
41: lineCount = inText.length;
42: to = inTo;
43: from = inFrom;
44: when = inWhen;
45: }
46: }
You should see the following output after you compile and run the ObjectWriter appli-
cation:
Object created successfully.
Object Input Streams
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.
 
Search WWH ::




Custom Search