Three ways to read the properties file in the resources directory

Keywords: Java Spring Junit encoding

In the development process, it is often necessary to read the configuration files under the resources directory. Most of them are automatically read by the framework. If we read them manually, how can we read them?

Here are three ways to read the properties file that I summarized:

Read configuration file based on InputStream
Get it through the PropertiesLoaderUtils tool class in Spring
Read through java.util.ResourceBundle class

preparation:

Create maven project properties read
Create data.properties in the resources directory. The data is as follows:

[XML] plain text view copy code
?

<dependencies>

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>RELEASE</version>
</dependency>
<!--IOC Dependent dependence-->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.0.6.RELEASE</version>
</dependency>

</dependencies>

Reading mode 1:

Read configuration file based on InputStream
[Java] plain text view copy code
?

/**

  • Read configuration file based on InputStream

*/
public class PropertiesTest1 {

private InputStream is;

@Test
public void proRead(){

    try {
        //Create Properties object
        Properties pro = new Properties();

        is = PropertiesTest1.class.getResourceAsStream("/data.properties");
        //Dealing with Chinese code disorder


        pro.load(is);

        Set<Map.Entry<Object, Object>> entries = pro.entrySet();
        for (Map.Entry<Object, Object> entry : entries) {

            String value =(String)entry.getValue();
            value = URLEncoder.encode(value, "ISO-8859-1");
            value = URLDecoder.decode(value, "GBK");

            System.out.println(entry.getKey()+"------"+value);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Reading mode 2:

Get it through the PropertiesLoaderUtils tool class in Spring
[Java] plain text view copy code
?

/**

  • Get it through the PropertiesLoaderUtils tool class in Spring

*/
public class PropertiesTest2 {

@Test
public void proRead(){

    try {
        Properties pro = PropertiesLoaderUtils.loadAllProperties("data.properties");


        Set<Map.Entry<Object, Object>> entries = pro.entrySet();

        for (Map.Entry<Object, Object> entry : entries) {

            String value =(String)entry.getValue();
            value = URLEncoder.encode(value, "ISO-8859-1");
            value = URLDecoder.decode(value, "GBK");

            System.out.println(entry.getKey()+"------"+value);
        }


    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

Reading mode 3:

Read through java.util.ResourceBundle class
[Java] plain text view copy code
?

/**

  • Read through java.util.ResourceBundle class

*/
public class PropertiesTest3 {

@Test
public void proRead(){

    ResourceBundle resourceBundle = ResourceBundle.getBundle("data");
    //Ergodic value
    Enumeration enumeration = resourceBundle.getKeys();
    while (enumeration.hasMoreElements()) {
        try {
            String key = (String) enumeration.nextElement();
            String value = resourceBundle.getString(key);

            value = URLEncoder.encode(value, "ISO-8859-1");
            value = URLDecoder.decode(value, "GBK");

            System.out.println(key+"------"+value);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

}

}

Conclusion:
The first reading method does not need to introduce a third-party jar package, and it is also well understood.
The second way is to introduce Spring's core dependency package. The code is relatively simple.
The third way to understand
The encoding method of the properties file is GBK, and the encoding of the read file is ISO-8859-1. We need to reverse encode and decode to deal with the problem of garbled code

Posted by nariman on Fri, 01 Nov 2019 14:46:15 -0700