Java Reference
In-Depth Information
74
System.out.println(
"After loading properties")
;
75
listProperties(props);
76
}
77
catch
(IOException ioException)
78
{
79
ioException.printStackTrace();
80
}
81
}
82
83
// output property values
84
private static void
listProperties(Properties props)
85
{
86
Set<Object> keys = props.keySet();
// get property names
87
88
// output name/value pairs
89
for
(Object key : keys)
90
System.out.printf(
91
"%s\t%s%n"
, key,
props.getProperty((String) key)
);
92
93
System.out.println();
94
}
95
}
// end class PropertiesTest
After setting properties
color blue
width 200
After replacing properties
color red
width 200
After saving properties
color red
width 200
After clearing properties
After loading properties
color red
width 200
Property color's value is red
Fig. 16.19
|
Properties
class of package
java.util
. (Part 3 of 3.)
Line 38 calls
Properties
method
getProperty
to locate the value associated with the
specified key. If the key is
not
found in this
Properties
object,
getProperty
returns
null
.
An overloaded version of this method receives a second argument that specifies the default
value to return if
getProperty
cannot locate the key.
Line 54 calls
Properties
method
store
to save the
Properties
object's contents to
the
OutputStream
specified as the first argument (in this case, a
FileOutputStream
). The
second argument, a
String
, is a description written into the file.
Properties
method
list
, which takes a
PrintStream
argument, is useful for displaying the list of properties.