Saving and Restoring Objects


A Java class can define a complicated data structure. An object can manipulate its data members using methods defined in its class such that the current values of its fields might be very different than when the object was first created. One nice feature about the Java I/O capability is that it defines a class that allows you to write the data associated with an object to an output stream. You can later restore the object by reading its data from an input stream. This capability can save you a lot of work. You no longer have to create the object's data structure from scratch.

As an example, in Chapter 23 a class named Polynomial was developed that stores as one of its data members the coefficients of an equation that models an ordinary differential equation. Rather than solving the ODE each time it was needed, it would be nice to solve it once and maintain the data in a Polynomial object that was stored on disk or onto a CD.

In this section, we will write a class named SaveObject that will save the data structure of a USatm76 object. The first thing the main() method of this class does is create a USatm76 object corresponding to the conditions using English units at an altitude of 40,000 feet. The method then creates an instance of the ObjectOutputStream class. We will save the object to a file, so we will wrap the ObjectOutputStream around a FileOutputStream connected to a file named "atm40kft.dat."

The data structure of the USatm76 object is saved by having the ObjectOutputStream call its writeObject() , passing the method a reference to the USatm76 object. The source code for the SaveObject class is shown here.

 import java.io.*; public class SaveObject {   public static void main(String args[]) {     USatm76 atm = new USatm76("English", 40000.0);     //  An ObjectOutputStream object is used     //  to write a USatm76 object to a file     try {       ObjectOutputStream writer =           new ObjectOutputStream(              new FileOutputStream("atm40kft.dat"));       writer.writeObject(atm);       writer.close();     } catch (IOException ioe) {       System.out.println("IO Exception occurred: " +                              ioe);       System.exit(1);     }   } } 

When you run this program, you will notice that a file named "atm40kft.dat" has been created on your system. Remember that only instances of classes that implement the Serializable interface can be saved in this manner.

The data structure of an object that has been saved using an ObjectOutputStream can be restored using an ObjectInputStream object. The RestoreObject class will restore the previously saved USatm76 object. An ObjectInputStream object is wrapped around a FileInputStream object that is associated with the file "atm40kft.dat." The ObjectInputStream object calls its readObject () method and the return value is cast into a USatm76 object. The data associated with the object is printed to standard output.

The source code of the RestoreObject class is shown next .

 import java.io.*; public class RestoreObject {   public static void main(String args[]) {     //  An ObjectInputStream object is used     //  to read a USatm76 object from a file     try {       ObjectInputStream reader =           new ObjectInputStream(              new FileInputStream("atm40kft.dat"));       USatm76 atm = (USatm76)reader.readObject();       //  Print out the data from the object.       String label[] = atm.getLabels();       System.out.println("\ngeometric altitude  = " +                          atm.getAltitude() + label[0]);       System.out.println("geopotential altitude = " +              atm.getGeoPotentialAltitude() + label[1]);       System.out.println("temperature           = " +                       atm.getTemperature() + label[2]);       System.out.println("pressure              = " +                          atm.getPressure() + label[3]);       System.out.println("molar mass            = " +                         atm.getMolarMass() + label[4]);       System.out.println("density               = " +                           atm.getDensity() + label[5]);       reader.close();     } catch (ClassNotFoundException cnfe) {       System.out.println("Class not found: " + cnfe);       System.exit(1);     } catch (IOException ioe) {       System.out.println("IO Exception occurred: " +                              ioe);          System.exit(1);     }   } } 

Output ”

 geometric altitude    = 40000.0 ft geopotential altitude = 39923.42988235446 ft temperature           = 389.97 R pressure              = 2.7300260343469853 psi molar mass            = 28.9645 lbm/lbmole density               = 0.018895018156005174 lbm/ft^3 


Technical Java. Applications for Science and Engineering
Technical Java: Applications for Science and Engineering
ISBN: 0131018159
EAN: 2147483647
Year: 2003
Pages: 281
Authors: Grant Palmer

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net