When writing a project, we often modify some configuration variables to adapt to different operating environment, but also allows users to leave the program to modify the relevant variable settings. Usually we define these variables in a file with a suffix named properties, and the contents of the file are formatted as "key = value" and the text is annotated with "#". and Java provides java.util.Properties to read these files, specific properties see the blog by the Sea: http://www.cnblogs.com/bakari/p/3562244.html 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 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.
To get the values defined in. Properties, you first load the configuration file with the method load (InputStream instream) of the properties to get the key-value pairs in the configuration file to form a list of attributes, followed by GetProperty ( String key) to get the corresponding value in the property list. There are two main ways to read. properties files (in Java project): Jdbc.properties:
Driverclass = Com.mysql.jdbc.Driver
jdbcurl=jdbc:mysql://127.0.0.1:3306/protest?useunicode=true& Characterencoding=utf-8
user = root
password = root
1. Using FileInputStream ("Resource Name Absolute path")
Public properties Propertiesfromfile () throws filenotfoundexception{
properties = new properties ();
InputStream in = null;
try{in
= new Bufferedinputstream (new FileInputStream (Propertiespath));
Properties.load (in);
} catch (FileNotFoundException e) {
e.printstacktrace ();
} catch (IOException e) {
e.printstacktrace ();
} finally{
try {
in.close ();
} catch (IOException e) {
e.printstacktrace ();
}
} return properties;
}
2. Use Getclass.getresourceasstream ("Resource Name")-Recommended
Public Properties Propertiesfromclass () {
properties = new properties ();
InputStream in1 = This.getclass (). getResourceAsStream (Propertiespath);
InputStream Is=propertiesload.class.getresourceasstream ("/jdbc.properties");
try {
properties.load (in1);
} catch (IOException e) {
e.printstacktrace ();
} finally{
try {
in1.close ();
} catch (IOException e) {
e.printstacktrace ();
}
} return properties;
}
Finally, use GetProperty (key) to get the values in properties:
Propertiesload proload = new Propertiesload (propertiespath);
String Driverclass = Proload.propertiesfromclass (). GetProperty ("Driverclass");
For modification of properties files in a program
Propertiesload proload = new Propertiesload (propertiespath);
Properties Pro = Proload.propertiesfromclass ();
OutputStream out = new FileOutputStream (DBconnect.class.getResource ("/jdbc1.properties"). toString (). SUBSTRING (6));
Pro.setproperty ("Password", password+ "1");
try {
Pro.store (out, "Ceshiwenjian01");
} catch (IOException e) {
e.printstacktrace ();
}
Results
Jdbc1.properties:
#ceshiwenjian01
#Tue Aug 16:06:07 gmt+08:00 2016
driverclass=com.mysql.jdbc.driver
user=root
PASSWORD=ROOT1
jdbcurl=jdbc\:mysql\://127.0.0.1\:3306/protest?useunicode\=true&characterencoding\= Utf-8
In the above example, if the properties Pro = Proload.propertiesfromclass (), replace the properties Pro = new properties (); Then the key value in the last Jdbc1.properties is left with only password. So the so-called setproperties (Key,value) is to write the key value pairs into pro and then pro through the store to write all the pro key value pairs into the file. The result shown in the previous example is similar to changing the value of a key value pair because pro loads the original key value pair.
Note: After we know how to read and write a property file, we still have a lot to pay attention to, because both the load and store methods read and write the property stream file in Iso-8859-1 encoding, and ILatin1 characters and some special characters, and for non Latin1 Characters and some special characters, you use a similar escape sequence for character and string literals to represent them in the form of values and elements. So when we are dealing with Chinese, we can not assign the Chinese value to the attribute when we modify the property file directly, but to give the attribute the value in Chinese by the SetProperty method in the Java program, because the store will convert Chinese to Unicode code. When reading, the system will print the read Unicode code according to the system encoding, for the Chinese system, usually GBK code, so that Chinese can be displayed normally.