Java Reference
In-Depth Information
screen with the
list()
method. The argument to
list()
is either a
PrintStream
or a
PrintWriter
.
System.out
is a
PrintStream
, so that
will work.
The format of the properties file is
name=value
. But it is also possible to
put comments in a properties file. Any line beginning with a “
#
” is ignored.
Try it.
It's also easy to (re)write a file of properties with the
store()
method.
The parameters are an
OutputStream
and a
String
; the latter will serve as a
label for the parameters, written to an opening comment in the properties file.
If your program needs to examine the list of property names, you can get
an
Enumerator
of the entire list via the
propertyNames()
method. Modify
Example 4.3 to replace the
list()
call with a do-it-yourself version that uses
the
Enumerator
returned from
propertyNames()
to list all the names and
values. Hint: Use
getProperty()
on each name retrieved via the enumeration.
The Java
Properties
class extends the
java.util.Hashtable
class.
This means, in part, that all the other
Hashtable
methods are available to a
Properties
class. Methods such as
containsKey()
or
containsValue()
can be helpful, as can
isEmpty()
. One caution, though. You should use
setProperty()
if you want to add values to
Properties
, rather than
the
Hashtable
's
put()
method. They do largely the same thing, but
setProperty()
enforces that its parameters are
String
s. This is important if
you want to write out the properties to a file, as it's meant for
String
s only.
T
HE
Runtime
C
LASS
4.5
Let's discuss one last way to get to the underlying (Linux) system information.
Be warned, though, that this is the least portable approach of all we have
mentioned.
exec()
4.5.1
Familiar to C/C++ programmers, the
exec()
call in the Java
Runtime
class
does much the same thing. It gives you a way to start another program outside
of the current Java Virtual Machine. In doing so, you can connect to its stan-
dard
in
/
out
/
err
and either drive it by writing to its standard
in
, or read its
results from its standard
out
. (Yes, that's correct—we write to its input and
read from its output. If that sounds wrong, think it through. Our Java code is