The java.util.Properties
class is a specialized hash table for strings. Properties are generally
used to hold textual configuration data. Examples of this are the Java
System properties, which are passed to a Java application on the command
line. We’ll cover those later in this section. More generally, you can use
a Properties table to hold arbitrary
configuration information for an application in an easily accessible
format. The neat thing about a Properties object is that it can load and store
its information in a plain text or XML text format using streams (see
Chapter 12 for information on streams).
Any string values can be stored as key/value pairs in a Properties table. However, the convention is to
use a dot-separated naming hierarchy to group property names into logical
structures. (Unfortunately, this is just a convention, and you can’t
really work with groups of properties in a hierarchical way as this might
imply.) For example, you can create an empty Properties object and add String key/value pairs just as you could with a
Map:
Propertiesprops=newProperties();props.setProperty("myApp.xsize","52");props.setProperty("myApp.ysize","79");
Thereafter, you can retrieve values with the getProperty()
method:
Stringxsize=props.getProperty("myApp.xsize");
If the named property doesn’t exist, getProperty() returns null. You can get an Enumeration of the property names with the
propertyNames()
method:
for(Enumeratione=props.propertyNames();e.hasMoreElements();){Stringname=e.nextElement();...}
When you create a Properties
object, you can specify a second object for default property
values:
Propertiesdefaults=...Propertiesprops=newProperties(defaults);
Now, when you call getProperty(),
the method searches the default table if it doesn’t find the named
property in the current table. An alternative version of getProperty() also accepts a default value; this
value is returned instead of null if
the property is not found in the current or default lists:
Stringxsize=props.getProperty("myApp.xsize","50");
You can save a Properties table to an OutputStream using the save() method. The
property information is output in a flat ASCII format. We’ll talk about
I/O in the next chapter, but bear with us for now. Continuing with the
previous example, output the property information using the System.out stream as follows:
props.save(System.out,"Application Parameters");
System.out is a standard output
stream that prints to the console or command line of an application. We
could also save the information to a file using a FileOutputStream as the first argument to
save(). The second argument to
save() is a String that is used as a header for the data.
The previous code outputs something like the following to System.out:
#ApplicationParameters#MonFeb1209:24:23CST2001myApp.ysize=79myApp.xsize=52
The load() method reads the
previously saved contents of a Properties object from an InputStream:
FileInputStreamfin;...Propertiesprops=newProperties()props.load(fin);
The list() method is useful
for debugging. It prints the contents to an OutputStream in a format that is more
human-readable but not retrievable by load(). It truncates long lines with an
ellipsis (...).
The Properties class also
contains storeToXML() and
loadFromXML() methods.
These operate just like the save()
and load() methods but write an XML
file like the following:
<?xmlversion="1.0"encoding="UTF-8"?><!DOCTYPEpropertiesSYSTEM"http://java.sun.com/dtd/properties.dtd"><properties><comment>MyProperties</comment><entrykey="myApp.ysize">79</entry><entrykey="myApp.xsize">52</entry></properties>
We’ll cover XML in detail in Chapter 24.
The java.lang.System
class provides access to basic system environment information through
the static System.getProperties()
method. This method returns a Properties table that contains system
properties. System properties take the place of environment variables in
some programming environments. Table 11-8 summarizes system properties that
are guaranteed to be defined in any Java environment.
Table 11-8. System properties
System property | Meaning |
|---|---|
Vendor-specific string | |
URL of vendor | |
Java version | |
Java installation directory | |
| Java class version |
The classpath | |
Operating system name | |
Operating system architecture | |
| Operating system version |
File separator (such as | |
| Path separator (such as |
| Line separator (such as |
| User account name |
User’s home directory |
Java applets and other Java applications that run with
restrictions may be prevented from reading the following properties:
java.home, java.class.path, user.name, user.home, and user.dir. As you’ll see later, these
restrictions are implemented by a SecurityManager object.
Your application can set system properties with the static method
System.setProperty() . You can also
set your own system properties when you run the Java interpreter, using
the -D option:
%java-Dfoo=bar-Dcat=BoojumMyApp
Because it is common to use system properties to provide
parameters such as numbers and colors, Java provides some convenience
routines for retrieving property values and parsing them into their
appropriate types. The classes Boolean, Integer, Long, and Color each come with a “get” method that looks
up and parses a system property. For example, Integer.getInteger("foo") looks for a system
property called foo and then returns
it as an Integer.