public class SerializeObjects
1: //SerializeObjects.java
2:
3: import java.io.*;
4:
5: public class SerializeObjects
6: {
7: public static void main(String[] args)
8: {
9: Junk obj1 = new Junk("A green twig is easily bent.");
10: Junk obj2 = new Junk("A little knowledge is a dangerous thing.");
11: Junk obj3 = new Junk("Flies light on lean horses.");
12: ObjectOutputStream objectOut = null;
13: try
14: {
15: //Create the object output stream
16: objectOut = new ObjectOutputStream(
17: new BufferedOutputStream(
18: new FileOutputStream("JunkObjects.bin")));
19:
20: //Write three objects to the file
21: objectOut.writeObject(obj1); //Write object
22: objectOut.writeObject(obj2); //Write object
23: objectOut.writeObject(obj3); //Write object
24: System.out.println("\n\nobj1:\n" + obj1
25: +"\n\nobj2:\n" + obj2
26: +"\n\nobj3:\n" + obj3);
27:
28: }
29: catch(IOException e)
30: {
31: e.printStackTrace(System.err);
32: System.exit(1);
33: }
34:
35: //Close the stream
36: try
37: {
38: objectOut.close(); //Close the output stream
39:
40: }
41: catch(IOException e)
42: {
43: e.printStackTrace(System.err);
44: System.exit(1);
45: }
46: }
47: }