1 Properties

1.6 Reading and Writing to Persistent Storage

Properties contain built-in support that make it easy to write and read their values to and from persistent storage.

First, PropertyInfo contains the methods

   void writeValue (Object value, PrintWriter pw,
                    NumberFormat fmt);
   Object scanValue (ReaderTokenizer rtok);

which allow an individual object value to written to a PrintStream or scanned from a ReaderTokenizer.

Second, if the host object maintains a PropertyList, it can use the convenience method

   void write (
       HasProperties host, PrintWriter pw, NumberFormat fmt);

to write out values for all properties for which getAutoWrite() returns true. Properties will be written in the form

   propertyName = value

where value is the output from the writeValue method of the PropertyInfo structure.

To economize on file space, there is another method which only writes out property values when those values differ from the property’s default value:

   boolean writeNonDefaults (
       HasProperties host, PrintWriter pw, NumberFormat fmt)

Again, values are written only for the properties for which getAutoWrite() returns true. The method returns false if not property values are written.

To read in property values, their are the methods

   boolean scanProperty (
       HasProperties host, ReaderTokenizer rtok);
   boolean scanSpecificProperty (
       HasProperties host, ReaderTokenizer rtok, String name);

where the former will inspect the input stream and scan in any recognized property of the form propertyName = value (returning true if such a property was found), while the latter will check the input for a property with a specific name (and return true if the specified property was found).