Developing Java Beans // the decrement button protected
Developing Java Beans some circumstances it requires very little extra programming effort to implement persistence, but it can often require extra work and careful planning. 5.1 Object Serialization The java.io package provides a streaming mechanism for serializing the persistent state of objects. Any type of input or output stream can be used, which allows persistence streams to be stored on a variety of storage mediums. The most common form of this is a file stream, using an instance of class java.io.FileOutputStream to save the data and class java.io.FileInputStream to restore it. Serialization can be broken down into streams and components. The stream is responsible for saving and restoring objects and primitive data types such as int and float. The components of serialization are those objects and data members that are saved and restored. The stream controls the process of saving objects by requesting that those objects write their own contents, and must contain enough information to reconstruct the object. The new instance should be of the same class or data type and should contain the same internal data values. The object stream contains the methods for saving or restoring objects and primitive data types. The java.io package defines two interfaces for this purpose. The java.io.ObjectOutput interface, which is implemented by objects that can save other objects to the stream; and the java.io.ObjectInput interface, which is implemented by objects that can restore other objects from the stream. The java.io.ObjectOutputStream and java.io.ObjectInputStream classes implement the java.io.ObjectOutput and java.io.ObjectInput interfaces, respectively. These classes provide methods for saving and restoring objects, as well as for saving and restoring the primitive data types. Here are two code snippets that show how these classes are used: // save a string and a double to the stream String str = “Sample”; double d = 3.14; FileOutputStream f = new FileOutputStream(”Beans.tmp”); ObjectOutputStream s = new ObjectOutputStream(f); s.writeObject(str); s.writeDouble(d); s.flush(); // restore the string and double FileInputStream f = new FileInputStream(”Beans.tmp”); ObjectInputStream s = new ObjectInputStream(f); String str = (String)s.readObject(); double d = s.readDouble(); In the first example we create an instance of the java.lang.String class and an instance of the primitive type double. Next we create an instance of java.io.FileOutputStream, and use it to create an instance of java.io.ObjectOutputStream. We then call the writeObject() method on the stream to save the string object, and call the writeDouble() method to save the double. Lastly, we call flush() to commit everything in the stream to the file. The second example reconstructs the objects from the file. An instance of java.io.FileInputStream is created and used to create an instance of java.io.ObjectInputStream. Next, the readObject() method is called on the stream to restore the string. Because the readObject() method does not distinguish between classes, the returned object must be cast to the appropriate type. Next, we call the readDouble() method to restore the value of the double. What’s not clear from the example is that there’s no call to the constructor (explicit or implicit) of an object being read back from the stream. page 71
Note: If you are looking for good and high quality web space to host and run your java application check Vision java hosting services