2. The main methods of the class are as follows:
3, mainly used to read the Java configuration file, storing some frequently used data, convenient for programmers to modify. The configuration file is a text file with a suffix (. properties),
The contents of the file are formatted as "Key=value", and text annotations can be commented using "#". Such as:
4, in the configuration file directly written in Chinese reading will be garbled, so to transcode into ASCII. The latest version of Eclipse will automatically transcode, and if manual transcoding is required, there are some online transcoding tools, which are recommended here:
Http://tool.oschina.net/encode?type=3
Second, Java properties instances
1. Get the input stream object from the target path test.properites
2. Use the Properties class's load () method to get the data from the byte input stream
3. Direct printing of Properties objects
4, using the Properties class GetProperty (String key) method, according to the parameter key to get value
5, the specific code is as follows:
Packageexample;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.net.URL;ImportJava.net.URLDecoder;ImportJava.net.URLEncoder;Importjava.util.Properties; Public classTest { Public Static voidMain (string[] args) {Try{Properties prop=NewProperties (); InputStream in= Test.class. getClassLoader (). getResourceAsStream ("Test.properties"); Prop.load (in); //Direct output Prop ObjectSYSTEM.OUT.PRINTLN ("Direct Output Prop object:" +prop); //get the value of nameString name=prop.getproperty ("name"); //get the value of addressString Address=prop.getproperty ("Address"); //get the value of the jobString job=prop.getproperty ("Job"); System.out.println ("Name=" +name+ ", address=" +address+ ", job=" +job); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } }}
6. The result of implementation is as follows:
As you can see, the value of the job is garbled, which means that you cannot use Chinese directly in the configuration file. And the note behind the # is not printed.