Java Summary of Properties usage (1), javaproperties
Recently, there is a need in the project to implement a scheduled task function, regularly back up database operation tables, and write table data into txt files. Because the file's read/write path may need to be changed at any time, it is not convenient to write the file to death or write it as a static variable. We will consider using the configuration file. Here we will summarize the usage of some configuration files.
I. Java Properties class
1. Java has an important class Properties (java. util. properties) is a persistent set of detailed attributes that can be saved to a stream or loaded from a stream. The following are key points about attributes:
-
Each key and its corresponding value in the attribute list is a string.
-
An attribute list can contain another attribute list as its "default". The second attribute can be searched in the list if the attribute key is not found in the original attribute list.
-
This class is thread-safe. Multiple Threads can share a Properties object without external synchronization.
-
2. The main methods of this class are as follows:
3. It is mainly used to read Java configuration files and store frequently-used data to facilitate programmer modification. The configuration file is a text file with the suffix (. properties ),
The file content format is "key = value". You can use "#" to annotate text annotations. For example:
4. garbled characters are generated when you directly write Chinese characters in the configuration file. Therefore, you must transcode the code to ASCII. The latest version of eclipse will automatically transcode. If you need manual transcoding, you can use some online transcoding tools. Here we recommend one:
Http://tool.oschina.net/encode? Type = 3
Ii. Java Properties instance
1. Obtain the input stream object from the target path test. properites.
2. Use the load () method of the Properties class to obtain data from the byte input stream
3. Print the Properties object directly.
4. Use the getProperty (String key) method of the Properties class to obtain the value based on the parameter key.
5. The specific code is as follows:
Package example; import java. io. IOException; import java. io. inputStream; import java.net. URL; import java.net. URLDecoder; import java.net. URLEncoder; import java. util. properties; public class Test {public static void main (String [] args) {try {Properties prop = new Properties (); InputStream in = Test. class. getClassLoader (). getResourceAsStream ("test. properties "); prop. load (in); // directly output the prop object System. out. println ("directly output prop object:" + prop); // obtain the value of name String name = prop. getProperty ("name"); // obtain the address value String address = prop. getProperty ("address"); // get the job value String job = prop. getProperty ("job"); System. out. println ("name =" + name + ", address =" + address + ", job =" + job);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
6. The execution result is as follows:
It can be seen that the job value is garbled, indicating that Chinese characters cannot be used directly in the configuration file. The comment after # is not printed.
Iii. Supplements to Path Problems
1. The Properties configuration file obtained above is in both Chinese and EnglishTest. class. getClassLoader (). getResourceAsStream ()Method to directly obtain the byte input stream. Therefore, you do not need to consider whether the path contains Chinese characters.Test. class. getClassLoader (). getResource () method,Because the return value of this method is URL, if the project directory contains a Chinese name, the obtained URL will be garbled, so use
String path=URLDecoder.decode(url.getFile(), "utf-8");
2. The specific code is as follows:
Package example; import java. io. fileInputStream; import java. io. IOException; import java. io. inputStream; import java.net. URL; import java.net. URLDecoder; import java.net. URLEncoder; import java. util. properties; public class Test {public static void main (String [] args) {try {Properties prop = new Properties (); Properties prop2 = new Properties (); // String path = Test. class. getClassLoader (). getResource ("example/china/test2.properties "). getPath (); // obtain the URL url = Test. class. getClassLoader (). getResource ("example/china/test2.properties"); // print the path to System. out. println ("url. getFile () = "+ url. getFile (); // transcode Chinese characters in the path String path = URLDecoder. decode (url. getFile (), "UTF-8"); System. out. println ("path =" + path); // obtain the byte input stream InputStream input = new FileInputStream (path) through the path; // directly obtain the byte input stream InputStream in = Test. class. getClassLoader (). getResourceAsStream ("example/china/test2.properties"); prop. load (in); prop2.load (input); System. out. println ("prop =" + prop); System. out. println ("prop2 =" + prop2);} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();}}}
3. output result: