Saving and retrieving objects with Java
Recently JavaSoft has made available an alpha release of the serialization and RMI classes. This new API is extremely useful and important to master. It allows developers to save and retrieve Java classes to any I/O stream. Objects can be saved and retrieved at later points in time. The supplied classes do not provide any namespace management for the saved objects. This article will cover:
- saving and retrieve objects to a local disk.
- saving objects over a network to a server on a known port.
- some performance metrics for you to think about.
- learn how to write Java programs that are both applications and applets โ where an application means it contains a
main().
Weโll conclude with some more discussion of the specifics of the implementation. This column teaches concepts and the applets donโt really do anything useful other than serve as an instructional vehicle.
The following examples demonstrate the use of the serialization technology that has been included in JDK 1.0.2. An add-on package is required to run the following applets. The applets below were run with the JDK appletviewer on Windows 95 and Solaris. I was unable to find a browser manufacturer that clearly stated what version of the JDK they were running and decided that it was best to wait for Microsoft and Netscape to get up to speed with the up and coming JDK 1.1 release. If you have problems installing the serialization classes needed on your system try looking at this FAQ. The javaSoft team has also been nice enough to collect all the mail messages for those of you wishing to find quick answers to questions.
Setting up the environment on your local machine
- Get and install the RMI classes following the directions provided with them. Make sure you set your
CLASSPATHenvironment variable to include the lib/rmi.zip file in the RMI directory. - Download and unzip the example files provided. Install these files into a directory and add it to
CLASSPATH. Use the appletviewer or the Java virtual machine to run the applets below.
Reading and writing Java objects to a local disk
The following two applets save an object and retrieve some classes. Because they are writing to the disk you will have to run these programs locally on your system. The ShowSavingAClass applet saves a String and a Date class to a disk file called SavedObject in the current directory. ShowReadingAClass reads the saved file SavedObject and recreates the classes contained in it. If your browser has RMI support then you can run the following two applets:
Writing Java objects to a server
The programs require that a server program be running on the server so that an applet or Java application can communicate over the port to save objects.
The program WriteObjectsFromSocket reads an object from a client and saves it on the server in a fixed area. Hereโs the source. To create a file on a server that contains an instance of the object run the following:
java WriteObjectsFromSocket servername portnumber,- The corresponding client piece is
ShowSavingAClassNet. Hereโs the source. Donโt forget to modify the HTML parameter tags if you host this applet on your local system.
The source code for ShowSavingAClassNet.java contains an example of designing applets that use the same source code for both so that users can start them with or without the applet viewer.
Performance considerations
The ShowWritedouble applet writes 10,000 double objects to a disk file. You can easily modify this to write other data types and get a feeling for how long it takes to save an object. This applet can easily be modified to retrieve the objects also.
Summary
When designing classes that may be serialized be aware of the fact that all methods and data will be saved so try to optimize space usage and practice encapsulation. There are some important security restrictions described in the documentation of the RMI classes. The special handling of private maintains Javaโs strict security model. Those of you wishing more information can take a look at reflection. The following questions arose during the development of these instructional applets:
- The setting of the acl.read or acl.write variables had no effect on the ability of an applet to read or write files on the local file system on my system. I was only able to get an applet to read or write files on the local file system when I included the appletโs class files in the
CLASSPATH. - It would be nice if the performance metric included serialization speed as a number.


