Read properties resource file in Java

Source: Internet
Author: User

First, through the ResourceBundle to read the. properties file
/** * Parse properties file by Java.util.resourceBundle. * @param the path to the string path:properties file * @param string key: Gets the property of the corresponding key * @return string: Returns the property of the corresponding key, empty when the failure. */public static string getPropertyByName1 (String path,string key) {    string result = null;    try {        result = Resourcebundle.getbundle (path). getString (Key). Trim ();    } catch (Exception e) {        E.printstacktrace ();    }    return result;}

For the completion of string path, be aware. It is generally divided into two situations:

1. properties file under the SRC directory, the file structure is as follows:

|src/

--test.properties

2. The. properties file is in a package below the SRC directory, because it is possible that we often get used to building various properties files in a package. The file structure is as follows:

|src/

|--configure/

| | --test1.properties

| | --test2.properties

For the first case, use the following method in the main function:

System.out.println (getconfigureinfo.getpropertybyname1 ("Test", "Key1"));

For the second case, use the following method in the main function:

System.out.println (getconfigureinfo.getpropertybyname1 ("Configure.test1", "Key1"));

In this usage, path does not have the. Properties suffix, and the direct input is fine.

Second, load the. properties file by getResourceAsStream mode
/** * Parse properties file. * @param the path to the string path:properties file * @param string key: Gets the property of the corresponding key * @return string: Returns the property of the corresponding key, NULL at the time of failure. */public string getPropertyByName2 (String path,string key) {    string result = null;        Properties Properties = new properties ();    try {        InputStream InputStream = This.getclass (). getClassLoader (). getResourceAsStream (path);        if (InputStream = = null) {            properties.load (inputstream);             result = Properties.getproperty (key). Trim ();             Inputstream.close ();        }    } catch (Exception e) {        e.printstacktrace ();    }    return result;  }

The same two cases:

1. The. properties file is below the SRC directory

Use this method:

Getconfigureinfo getconfigureinfo = new Getconfigureinfo (); System.out.println (Getconfigureinfo.getpropertybyname2 ("Test1.properties", "Key1"));

2. The. properties file is in a package below the SRC directory:

How to use:

Getconfigureinfo getconfigureinfo = new Getconfigureinfo (); System.out.println (Getconfigureinfo.getpropertybyname2 ("Configure/test1.properties", "Key1"));

You can see that this kind of file-like reading and writing is obvious, so the usage is different.

Third, write the test.properties file using FileOutputStream and propertity:
/** * Write to the. properties file, Key=content * @param string path: Write file path * @param string key: Write key * @param string content: Pair of key written Contents * @return void */public void SetProperty (String path,string key,string content) {    Properties properties = New Prope Rties ();    try {        Properties.setproperty (key, content);        True indicates append.        if (new File). Exists ()) {            FileOutputStream fileoutputstream = new FileOutputStream (path, true);            Properties.store (FileOutputStream, "Just for a test of write");            System.out.println ("Write done");            Fileoutputstream.close ();        }    } catch (Exception e) {        e.printstacktrace ();    }}

How to use:

Getconfigureinfo getconfigureinfo = new Getconfigureinfo (); Getconfigureinfo.setproperty ("Src/testConfig.properties "," Test3 "," test3 value ");

It is worth noting that now the path in this function is actually added src/, because it is used FileOutputStream, so the default main path is the project path.

Summarize:

For Java, I think these paths are very unreasonable, and now it seems that the main path is under the project when using the input/output stream to read and write files.

For ResourceBundle, the path to read the properties file is not added. The properties are also very peculiar. It seems that there are different ways to load files in Java with the default master path (or root directory).

The root directory also determines how the path should be written to be recognized by the program. I do not yet have a record of these differences.

If you have anything to correct and educate me, please point out that the younger brother wants to know exactly how to look.

Read properties resource file in Java

Related Article

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.