![]() |
Properties is a structure that store variables, as in a
hash table in a way that can serve as a mini-database. As in .ini
files, this mini-database is useful in saving default settings.
Properties is equipped with methods that can save a list
of values of variables in a file in plain ASCII format, or even
as an XML file.
The usage: construct the Properties and call
loadFromXML to read the variables from a file. Once all
the variables are saved safely in a file, no more .ini files -- the
variables can be retrieved from the Property class by
getProperty.
The following code reads variable values from an XML file and store
them in the Properties.
1 Properties properties=new Properties();
2
3 public void readProperties()
4 {
5 FileInputStream in=null;
6 try
7 {
8 File file=new File("db.properties");
9 if(file.exists())
10 {
11 in=new FileInputStream(file);
12 properties.loadFromXML(in);
13 }
14 }
15 catch(Exception e){e.printStackTrace();}
16 finally
17 {
18 try
19 {
20 if(in!=null)
21 in.close();
22 }
23 catch(Exception e){e.printStackTrace();}
24 }
25
26 String property="";
27 if((property=properties.getProperty("dbName"))==null || property.equals(""))
28 properties.setProperty("dbName"," ");
29 if((property=properties.getProperty("tableName"))==null || property.equals(""))
30 properties.setProperty("tableName"," ");
31 if((property=properties.getProperty("driver"))==null || property.equals(""))
32 properties.setProperty("driver","com.mysql.jdbc.Driver");
33 if((property=properties.getProperty("openDBString"))==null || property.equals(""))
34 properties.setProperty("openDBString","jdbc:mysql://localhost/");
35 if((property=properties.getProperty("user"))==null || property.equals(""))
36 properties.setProperty("user","easai");
37 if((property=properties.getProperty("password"))==null || property.equals(""))
38 properties.setProperty("password","");
39
40 if((property=properties.getProperty("debug"))!=null && !property.equals(""))
41 debug=property.equals("true")?true:false;
42 else
43 properties.setProperty("debug","false");
44 }
The latter half of the above code shows how the variables are set to the
default values when they are not specified in the XML file. With
Properties, there is no need to keep all the variables
separately. They are all in the Properties class.
Call storeToXML to save the variables. The first
argument of the function is the output file, and the second argument
is a comment. In the code below, the comment is set to
DateFormat string.
1 void writeProperties()
2 {
3 FileOutputStream out=null;
4 try
5 {
6 out=new FileOutputStream("db.properties");
7 java.text.DateFormat dateFormat
8 =java.text.DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.FULL);
9 properties.storeToXML(out,dateFormat.format(new java.util.Date()));
10 }
11 catch(Exception e){e.printStackTrace();}
12 finally
13 {
14 try
15 {
16 if(out!=null)
17 out.close();
18 }
19 catch(Exception e){e.printStackTrace();}
20 }
21 }
Now let's look at the actual usage of the above functions in a real
Java application called ImportFiles.
ImportFiles is a
simple application that moves selected files to the destination
directory. In ImportFiles.java, the class variables
destDir and filesToImport are loaded and
will be saved as properties in the file
(ImportFiles.properties).
System.getProperty("user.dir") will return the
user home directory, such as c:/Documents and
Settings/easai, which is a good place to put the property
files.