ptg7068951
294 HOUR 20: Reading and Writing Files
. Call store(OutputStream, String)to save the properties to the
designated output stream with a description of the properties file as
the string.
For the next project, you build on the ConfigWriterapplication, which
wrote several program settings to a file. The Configuratorapplication
reads those settings into a Java properties file, adds a new property named
runtimewith the current date and time, and saves the altered file.
Create a new empty Java file to hold the Configuratorclass and enter the
text from Listing 20.4.
LISTING 20.4 The Full Text of Configurator.java
1: importjava.io.*;
2: importjava.util.*;
3:
4: classConfigurator {
5:
6: Configurator() {
7: try {
8: // load the properties file
9: File configFile = new File(“program.properties”);
10: FileInputStream inStream = new
➥FileInputStream(configFile);
11: Properties config = new Properties();
12: config.load(inStream);
13: // create a new property
14: Date current = new Date();
15: config.setProperty(“runtime”, current.toString());
16: // save the properties file
17: FileOutputStream outStream = new
➥FileOutputStream(configFile);
18: config.store(outStream, “Properties settings”);
19: inStream.close();
20: config.list(System.out);
21: } catch (IOException ioe) {
22: System.out.println(“IO error “+ ioe.getMessage());
23: }
24: }
25:
26: public static voidmain(String[] arguments) {
27: Configurator con = new Configurator();
28: }
29: }
The output of the Configuratorapplication is shown in Figure 20.3.