Home > Java > javaTutorial > Detailed explanation of how Java programs read properties configuration files

Detailed explanation of how Java programs read properties configuration files

高洛峰
Release: 2017-01-12 10:32:35
Original
1148 people have browsed it

When we usually write programs, some parameters change frequently, and this change is not something we predict. For example, we have developed a module for operating database. During development, we connect to the local database, so the IP, database name, table name, database host and other information are local to us. To make this module for operating data universal, then The above information cannot be written into the program. Usually our approach is to use configuration files to solve it.
Each language has its own supported configuration file types. For example, Python supports .ini files. Because it has an internal ConfigParser class to support reading and writing of .ini files, programmers can freely operate .ini files according to the methods provided by this class. In Java, Java supports reading and writing of .properties files. The JDK's built-in java.util.Properties class provides convenience for us to operate .properties files.

one. .properties file format

# 以下为服务器、数据库信息
dbPort = localhost
databaseName = mydb
dbUserName = root
dbPassword = root
# 以下为数据库表信息
dbTable = mytable
# 以下为服务器信息
ip = 192.168.0.9
Copy after login

In the above file we assume that the file name is: test.properties file. The line starting with # is annotation information; the one on the left side of the equal sign "=" is called key; the one on the right side of the equal sign "=" is called value. (In fact, it is what we often call key-value pairs) key should be a variable in our program. The value is configured according to the actual situation.

two. Properties class in JDK

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

3. Java reading properties file example
There is a properties file box.properties, the content is as follows:

Color=Red
Name=Box
Length=18
Width=7
Heigth=8
Copy after login

Get the Attribute value, the following code can be used:

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));
}
Copy after login

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));
  }
}
Copy after login

More detailed explanation of Java program to read properties configuration For articles related to file methods, please pay attention to the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template