Java Summary of Properties usage

Source: Internet
Author: User

In the recent project, there is a requirement to do a timed task function, to back up the database schedule, and to write the table data to the TXT file. Because the file read-write path may need to be changed at any time, so it is not convenient to write dead or written static variables, consider using configuration file, here summarizes some configuration file usage.

First, Java Properties class

1, Java has a more important class properties (Java.util.Properties), is a persistent set of detailed properties, properties can be saved to a stream or load from the stream class. The following are the key points about attributes:

  • Each key and its corresponding value in the property list is a string.

  • One property list can contain another property list as its "default", and the second property can be searched in the list if there is no property key found in the original property list.

  • This class is thread-safe, and multiple threads can share a properties object without requiring external synchronization

  • 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.

Iii. additions to the problem of routes

1. The above obtained properties configuration file in English is through the Test.class.getClassLoader (). getResourceAsStream () method to get the byte input stream directly, So do not consider whether the path contains Chinese, if it is through the Test.class.getClassLoader (). GetResource () method, because the method return value is a URL, if the project has a Chinese name in the directory, The URL you get is garbled, so use the

String Path=urldecoder.decode (Url.getfile (), "utf-8");

2, the specific code is as follows:

 Packageexample;ImportJava.io.FileInputStream;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 (); Properties PROP2=NewProperties ();//String Path =test.class.getclassloader (). GetResource ("Example/china/test2.properties"). GetPath (); //Get URL pathURL Url=test.class. getClassLoader (). GetResource ("Example/china/test2.properties"); //Print PathSystem.out.println ("url.getfile () =" +url.getfile ()); //to transcode Chinese in a pathString Path=urldecoder.decode (Url.getfile (), "Utf-8"); System.out.println ("Path=" +path); //To get a byte input stream through a pathInputStream input=NewFileInputStream (path); //direct access to byte input streamInputStream 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 blockE.printstacktrace (); }    }}

3. Output Result:

Java Summary of Properties usage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.