Java Reference
In-Depth Information
Yo u can create your own properties tables to maintain, for example, the config-
uration settings for a large complex program with many parameters and options.
The tables can be saved to or loaded from a file. However, with J2SE 1.4 came the
more elaborate Preferences API for this task, which we discuss in the following
section.
The HashMap class came with the Collections Framework in Java 1.2. It
provides essentially the same capabilities as Hashtable except that it can store
null references and its methods are not synchronized. When synchronization
is not needed, then the HashMap is to be preferred over Hashtable since the
former performs faster. See Section 10.6 for more information about Collections.
10.5 Preferences
The Preferences API is one of those small features in the Java 2 class library that
is easy to ignore as unimportant but yet proves to be enormously helpful when its
services are needed. Preferences are unlikely to be useful for custom scientific
programs that you write purely for your own purposes, since you will probably
just hard-code all your preferences into the code itself. However, if an application
has more than one user, you can be sure that some of the users will wish to modify
the default choices for such things as font style, size, and color or window sizes.
An application might provide a “Font” menu in which the font style and size is
controlled and so go a long way toward providing some of the control that users
desire. But your users will be disappointed to find that the application reverts to
your hard-coded choices each time the application is restarted.
One solution to this problem is to use a configuration file that a user edits to
configure an application. To use such a configuration file, however, introduces
several difficulties. First, the user must manually edit the file and this means that
he must learn the format of the file, get all the spelling exactly correct, know the
proper place to store the file, etc. Second, the application must find and read the
configuration file. Devising a platform-independent scheme to specify where a
configuration file is located in a file system is not a problem with an obvious or easy
solution, though Java's built-in system properties user.home and user.name
offer some help. It is also not trivial to invent a format for a configuration file, or
to write the code to read and parse that format, or to document the chosen format
so that users will know how to create and edit the file. And you must also deal with
configuration preferences of multiple users of the same application on the same
machine. So storing a single configuration file is not sufficient since multiple
users will overwrite each other's preferences. A valuable technique used in the
past to handle the formatting, reading, and writing of configuration information is
the java.util.Properties class, mentioned in the previous section, and its
load() and store() methods. The Properties class has been a part of Java
since version 1.0. Still, the problem of knowing where to store the configuration
file remains.
Search WWH ::




Custom Search