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
:
Properties
props
=
new
Properties
();
props
.
setProperty
(
"myApp.xsize"
,
"52"
);
props
.
setProperty
(
"myApp.ysize"
,
"79"
);
Thereafter, you can retrieve values with the getProperty()
method:
String
xsize
=
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
(
Enumeration
e
=
props
.
propertyNames
();
e
.
hasMoreElements
();
)
{
String
name
=
e
.
nextElement
();
...
}
When you create a Properties
object, you can specify a second object for default property
values:
Properties
defaults
=
...
Properties
props
=
new
Properties
(
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:
String
xsize
=
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
:
#
Application
Parameters
#
Mon
Feb
12
09
:
24
:
23
CST
2001
myApp
.
ysize
=
79
myApp
.
xsize
=
52
The load()
method reads the
previously saved contents of a Properties
object from an InputStream
:
FileInputStream
fin
;
...
Properties
props
=
new
Properties
()
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:
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!
DOCTYPE
properties
SYSTEM
"http://java.sun.com/dtd/properties.dtd"
>
<
properties
>
<
comment
>
My
Properties
</
comment
>
<
entry
key
=
"myApp.ysize"
>
79
</
entry
>
<
entry
key
=
"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
=
Boojum
MyApp
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
.
Get Learning Java, 4th 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.