Exercises

  • Exercise 10-1. The java.util.Properties class is essentially a hashtable that maps string keys to string values. It defines store( ) and load( ) methods that save and load its contents to and from a byte stream. These methods save the Properties object in a human-readable text format despite the fact that they use byte streams. Properties inherits the Serializable interface from its superclass, java.util.Hashtable. Define a subclass of Properties that implements storeBinary( ) and loadBinary( ) methods that use object serialization to save and load the contents of the Properties object in binary form. You may also want to use java.util.zip.GZIPOutputStream and java.util.zip.GZIPInputStream in your methods to make the binary format particularly compact. Use the serialver program to obtain a serialVersionUID value for your class.

  • Exercise 10-2. As noted in the previous exercise, the Properties object has store( ) and load( ) methods that allow it to save and restore its state. Define an Externalizable subclass of Properties that uses these store( ) and load( ) methods as the basis for its writeExternal( ) and readExternal( ) methods. In order to make this work, the writeExternal( ) method needs to determine the number of bytes written by the store( ) method and write this value into the stream before writing the bytes themselves. This allows the readExternal( ) method to know when to stop reading. Also include a version number in the externalizable data format you use.

Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.