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.
The form of a. properties file
==========================================================
# 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 Properties classes in JDK
The Properties class exists in cell Java.util, which inherits from Hashtable and 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 write the list of properties in this properties table (key and element pairs) 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 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;
public class Configuration
... {
Private Properties Propertie;
Private FileInputStream Inputfile;
Private FileOutputStream outputfile;
Public Configuration ()
... {
Propertie = new Properties ();
}
Public Configuration (String FilePath)
... {
Propertie = new Properties ();
Try ... {
Inputfile = new FileInputStream (FilePath);
Propertie.load (Inputfile);
Inputfile.close ();
catch (FileNotFoundException ex) ... {
System.out.println ("Read Property file---> failed. -Cause: File path error or file not present ");
Ex.printstacktrace ();
catch (IOException ex) ... {
SYSTEM.OUT.PRINTLN ("Load file---> Fail!");
Ex.printstacktrace ();
}
}//end readconfiginfo (...)
public string GetValue (string key)
... {
if (Propertie.containskey (key)) ... {
String value = Propertie.getproperty (key);//Get the value of a property
return value;
}
Else
Return "";
}//end GetValue (...)
public string GetValue (string fileName, String key)
... {
Try ... {
String value = "";