Developing Java Beans Example5.tmp. In both cases the
Developing Java Beans Example5.tmp. In both cases the dump() method is called to print the Value property of all of the objects. Here’s the code for this application: import java.io.*; public class Example5 { // the application entry pointpublic static void main(String[] args) { if (args[0].equals(”save”)) { Container c = new Container(); c.setValue(13); try { FileOutputStream f = new FileOutputStream(”Example5.tmp”); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject(c); s.flush(); } catch (Exception e) { System.out.println(e); } c.dump(); } else if (args[0].equals(”restore”)) { try { FileInputStream f = new FileInputStream(”Example5.tmp”); ObjectInputStream s = new ObjectInputStream(f); Container c = (Container)s.readObject(); c.dump(); } catch (Exception e) { System.out.println(e); } } } } Run the application with the following command: java Example5 save The application first creates an instance of class Container, and sets its Value property to 13. This object creates instances of WidgetA and WidgetB, and then sets their Value properties accordingly. Next an instance of java.io.FileOutputStream is created for the file named Example5.tmp, which is used to create an instance of java.io.ObjectOutputStream. This stream is used to write the instance of class Container. All of the objects that are reachable from that object are also written automatically. Finally, the dump() method of the Container object is invoked, resulting in the following output: 13:26:39 page 77