When we write programs, some parameters are often changed, but this change is not what we predict. For example, we have developed a module that operates the database. During development, we connect to the local database and the IP, database name, table name, database host and other information are local. To make the module that operates the data universal, the above information cannot be written in the program. Usually our approach is to use configuration files to solve it.
Various languages have their own supported configuration file types. For example, Python, it supports .ini files. Because it has a ConfigParser class inside to support the reading and writing of .ini files, programmers can freely operate .ini files according to the methods provided by this class. In Java, Java supports the reading and writing of .properties files. The built-in java.util.Properties class of JDK provides us with convenience in operating .properties files.
one. .properties file form
# The following is 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 that the file name is: test.properties file. The first behavior of # comments the information; on the left side of the equal sign "=" we call it key; on the right side of the equal sign "=" we call it value. (In fact, it is what we often call key-value pairs) key should be a variable in our program. And value is configured according to the actual situation.
two. Properties class in JDK
The Properties class exists in the cell Java.util, which inherits from Hashtable, and it provides several main methods:
1. getProperty(String key), search for properties in this property list with the specified key. That is, through the parameter key, we get the value corresponding to the key.
2. load(InputStream inStream), reads the attribute list (key and element pair) from the input stream. Get all key-value pairs in the file by loading the specified file (such as the test.properties file above). For getProperty(String key) to search.
3. setProperty(String key,String value), call Hashtable method put. It sets the value key-value pair by calling the put method of the base class.
4. store(OutputStream out,String comments), writes the property list (key and element pair) in this Properties table to the output stream in a format suitable for loading into the Properties table using the load method. In contrast to the load method, this method writes key-value pairs to the specified file.
5. clear(), clear 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. Java reading properties file example
There is a properties file box.properties, with the following content:
Color=RedName=BoxLength=18Width=7Heigth=8
To get the attribute 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)); }}However, the output order is different from the original file.