Exercises
Exercise 10-1. The
java.util.Properties
class is essentially a hashtable that maps string keys to string values. It definesstore( )
andload( )
methods that save and load its contents to and from a byte stream. These methods save theProperties
object in a human-readable text format despite the fact that they use byte streams.Properties
inherits theSerializable
interface from its superclass,java.util.Hashtable
. Define a subclass ofProperties
that implementsstoreBinary( )
andloadBinary( )
methods that use object serialization to save and load the contents of theProperties
object in binary form. You may also want to usejava.util.zip.GZIPOutputStream
andjava.util.zip.GZIPInputStream
in your methods to make the binary format particularly compact. Use the serialver program to obtain aserialVersionUID
value for your class.Exercise 10-2. As noted in the previous exercise, the
Properties
object hasstore( )
andload( )
methods that allow it to save and restore its state. Define anExternalizable
subclass ofProperties
that uses thesestore( )
andload( )
methods as the basis for itswriteExternal( )
andreadExternal( )
methods. In order to make this work, thewriteExternal( )
method needs to determine the number of bytes written by thestore( )
method and write this value into the stream before writing the bytes themselves. This allows thereadExternal( )
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.