When we write the program, some of the parameters are often changed, and this change is not what we foresee. For example, we developed a module to operate the database, in the development of our connection to the local database so IP, database name, table name, database host and other information is our local, to make this operation of the data module has universality, then the above information can not be written dead in the program. Usually our approach is to use a configuration file to solve the problem.
Various languages have their own profile types that are supported. Like Python, he supports the. ini file. Because he has an internal configparser class to support the reading and writing of the. ini file, the method provided by this class allows programmers to operate the. ini file freely. In Java, Java supports the reading and writing of. Properties files. The JDK built-in Java.util.Properties class facilitates our operation of the. Properties file.
one. . Properties file in the form of ==========================================================
# The following is server, database information
Dbport = localhost
DatabaseName = MyDB
Dbusername = root
Dbpassword = root
# The following is the database table information
DBTable = MyTable
# The following is server information
IP = 192.168.0.9
······
In the above file we assume that the file name is: Test.properties file. Where # begins a behavior annotation information, which we call the key on the left side of the equal sign "=" and what we call value on the right side of the equal sign "=". (In fact, we often say that key-value pairs) key should be the variables in our program. And value is based on the actual situation we configure.
two. the Properties class properties in JDK exist in cell Java.util, which inherits from Hashtable, It provides several main methods: 1. GetProperty ( String key) to search for properties in this property list with the specified key. That is, the value of the key is obtained by the key of the parameter.
2. load ( inputstream instream), which reads the list of attributes (key and element pairs) from the input stream. Gets all key-value pairs in the file by loading the specified file, such as the Test.properties file above. To search for getproperty ( String key). 3. setProperty ( string key, string value), call the Hashtable method put. He sets the key-value pair by calling the base class's put method.
4. store ( outputstream out, String comments) to fit the use of l The Oad method is loaded into a format in the Properties table, and the list of properties in this properties table (key and element pairs) is written to the output stream. In contrast to the load method, this method writes a key-value pair to the specified file.
5. Clear (), clears all loaded key-value pairs. This method is provided in the base class.
With these several methods, we can operate on the. Properties file.
three. Code Instances
Package configuration;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import java.util.Properties;
/** */ /**
* Read Properties file
* @author Qutr
*
*/
public class Configuration
... {
Private Properties Propertie;
Private FileInputStream Inputfile;
Private FileOutputStream outputfile;
/** *//**
* Initialize Configuration Class
*/
Public Configuration ()
... {
Propertie = new Properties ();
}
/** *//**
* Initialize Configuration Class
* @param filePath the path + name of the configuration file to read
*/
Public Configuration (String FilePath)
... {
Propertie = new Properties ();
Try ... {
Inputfile = new FileInputStream (FilePath);
&