Java Reference
In-Depth Information
21
22
// replace property value
table.setProperty(
"color"
,
"red"
);
23
24
25
System.out.println(
"After replacing properties"
);
26
listProperties(table);
27
28
saveProperties(table);
29
30
table.clear();
// empty table
31
32
System.out.println(
"After clearing properties")
;
33
listProperties(table);
34
35
loadProperties(table);
36
37
// get value of property color
Object value = table.getProperty(
"color")
;
38
39
40
// check if value is in table
41
if
(value !=
null
)
42
System.out.printf(
"Property color's value is %s%n"
, value);
43
else
44
System.out.println(
"Property color is not in table"
);
45
}
46
47
// save properties to a file
48
private static void
saveProperties(Properties props)
49
{
50
// save contents of table
51
try
52
{
53
FileOutputStream output =
new
FileOutputStream(
"props.dat"
);
54
props.store(output,
"Sample Properties"
);
// save properties
55
output.close();
56
System.out.println(
"After saving properties"
);
57
listProperties(props);
58
}
59
catch
(IOException ioException)
60
{
61
ioException.printStackTrace();
62
}
63
}
64
65
// load properties from a file
66
private static void
loadProperties(Properties props)
67
{
68
// load contents of table
69
try
70
{
71
FileInputStream input =
new
FileInputStream(
"props.dat"
);
72
props.load(input);
// load properties
73
input.close();
Fig. 16.19
|
Properties
class of package
java.util
. (Part 2 of 3.)