Explain how the Java program reads the properties configuration file

  • 2020-05-09 18:40:51
  • OfStack

When we usually write a program, some parameters are often changed, and this change is not expected. For example, we have developed a module for database operation. When we connect to the local database during the development, the information such as IP, database name, table name and database host is local to us. In order to make the module for data operation universal, the above information cannot be written in the program. The usual approach is to use configuration files.
Each language has its own supported profile type. For example, Python, he supports the.ini file. Because it has an internal ConfigParser class that supports reading and writing to.ini files, programmers are free to manipulate the.ini files based on the methods provided by this class. In Java, Java supports reading and writing of.properties files. The built-in JDK class java.util.Properties provides convenience for us to manipulate the.properties file.

1. .properties file format


#  The following are the server and database information 
dbPort = localhost 
databaseName = mydb 
dbUserName = root 
dbPassword = root 
#  The following is the database table information 
dbTable = mytable 
#  The following is the server information 
ip = 192.168.0.9 

In the above file we assume the file name is: test.properties. Where # begins with 1 behavior annotation information; On the left hand side of the equal sign, we call it key; The one on the right of the equals sign is called value. key should be a variable in our program. value is configured according to the actual situation.

2. Properties class in JDK

The Properties class exists in the cell Java.util, which inherits from Hashtable, and provides several main methods:
1. getProperty(String key),   searches the property list with the specified key. That is, value corresponding to key is obtained through the parameter key.
2. load(InputStream inStream), reads the list of attributes (key and element pairs) from the input stream. Get all key-value pairs in a specified file by loading it, such as the       test.properties file above. Search for getProperty(String key).
3. setProperty(String key,String value), which calls the put method of Hashtable. He sets the key-value pair by calling the put method of the base class.
4. store(OutputStream out,String comments),   writes the list of attributes (key and element pairs) from this Properties table to the output stream in a format suitable for loading into the Properties table using the load method. This method, in contrast to the load method, writes key-value pairs to the specified file.
5. clear(), which clears all loaded key-value pairs. This method is provided in the base class.
With the above methods, we can operate on the.properties file!

3. Sample properties files read by Java
There is one properties file, box.properties, which reads as follows:


Color=Red
Name=Box
Length=18
Width=7
Heigth=8

To get the property value, you can use the following code:


InputStream in = null;
Properties p = new Properties();
try {
  in = new BufferedInputStream(new FileInputStream("box.properties"));
  p.load(in);
} catch (FileNotFoundException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
Enumeration<Object> keys = p.keys();
while (keys.hasMoreElements()) {
  String key = (String) keys.nextElement();
  System.out.println(key + " : " + p.getProperty(key));
}

Or:


InputStream in;
ResourceBundle rb = null;
try {
  in = new BufferedInputStream(new FileInputStream("box.properties"));
  rb = new PropertyResourceBundle(in);
} catch (FileNotFoundException e1) {
  // TODO Auto-generated catch block
  e1.printStackTrace();
} catch (IOException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
}
if (rb != null) {
  Enumeration<String> keys = rb.getKeys();
  while (keys.hasMoreElements()) {
    String key = (String) keys.nextElement();
    System.out.println(key + " : " + rb.getString(key));
  }
}

  but the output order is different from the original file.


Related articles: