Web Hosting Tomcat, Java, JSP, Servlets, J2EE, PHP, MySQL

Cheap web hosting provider

05
Jan

Chapter 6: Case Study: The Library Application Revisited

Chapter 6: Case Study: The Library Application Revisited // try { FileOutputStream fos = new FileOutputStream( Book.ser ); // open persistent file ObjectOutputStream oos = new ObjectOutputStream(fos); // wrap with decorator oos.writeObject(bk); // now save instance oos.close(); // close persistent file } catch(IOException ex) { // report error } } // method: main } // class: MainSave Restoring the object during a subsequent execution of the application is equally simple. The readObject method call from the ObjectInputStream class is all we require. Since this method is used to restore any serialized object, then we must cast it to the correct type. We then have a Book object containing the same state information as the original. public class MainLoad { // —– Operations ———public static void main(String[ ] args) { try { FileInputStream fis = new FileInputStream( Book.ser ); // open persistent file ObjectInputStream ois = new ObjectInputStream(fis); // wrap with decorator Book bk = (Book)ois.readObject(); // restore the instance ois.close(); // close persistent file Console.out.println( Title: + bk.getTitle()); // confirm all is ok } catch(IOException ex) { // report error } catch(ClassNotFoundException ex) { // report error } } // method: main } // class: MainLoad We should note that when we serialize an object then not only that object s state is made to persist, but also all persistent objects referenced by it. Thus if our model is a Library object with its stock comprising many Book objects, then when we persist the Library object then so too do all the associated Book objects. Similarly, when we restore the single Library object, as in the second illustration above, then the Book objects are also restored as the library stock. 6.4.3 Design Applying this revision to iteration 2 of this case study is relatively straight-forward. First, we arrange that the classes LoanItemImp, LenderImp and BorrowerRecordImp

If you looking for unlimited one inclusive web hosting plan please check unlimited web hosting website.

Leave a Reply

Home | About Us | Compare Plans | Standard Hosting | Business Hosting | Java Tomcat Hosting | Support | FAQ | Order Now!

Powered by Omnicus java hosting services. All rights reserved.
Entries (RSS) and Comments (RSS).