读取resources目录下properties文件的三种方式

在Java开发中,通常需要从resources目录读取properties文件。本文介绍了三种方法:1) 基于InputStream;2) 使用Spring的PropertiesLoaderUtils;3) 利用ResourceBundle。文中还提及了如何处理编码问题,如GBK编码的properties文件在使用ISO-8859-1读取时可能出现的乱码情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在开发过程中常常会需要读取resources目录下的配置文件,大部分是框架帮我们自动读取,如果我们手动读取该如何读取呢?

 

以下是个人总结的读取properties文件的三种方式:

 

  • 基于InputStream读取配置文件
  • 通过Spring中的PropertiesLoaderUtils工具类进行获取
  • 通过 java.util.ResourceBundle 类读取



准备工作:

 

  • 创建maven项目properties_read
  • 在reaources目录下创建data.properties,数据如下:

   

[XML] 纯文本查看 复制代码
?
1
2
3
4
5
username=张无忌
age=18
position=明教第34代教主
education=武当大学太极训练班
address=光明顶

 

  • 在pom.xml中引入以下依赖
[XML] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>RELEASE</version>
    </dependency>
    <!--IOC相关依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.0.6.RELEASE</version>
    </dependency>
</dependencies>



读取方式一:

基于InputStream读取配置文件

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/**
 * 基于InputStream读取配置文件
 */
public class PropertiesTest1 {
 
    private InputStream is;
 
    @Test
    public void proRead(){
 
        try {
            //创建Properties对象
            Properties pro = new Properties();
 
            is = PropertiesTest1.class.getResourceAsStream("/data.properties");
            //处理中文乱码
 
 
            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();
        }
    }
}



读取方式二:

通过Spring中的PropertiesLoaderUtils工具类进行获取

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
 * 通过Spring中的PropertiesLoaderUtils工具类进行获取
 */
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();
        }
    }
}



读取方式三:

通过 java.util.ResourceBundle 类读取

[Java] 纯文本查看 复制代码
?
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/**
 * 通过 java.util.ResourceBundle 类读取
 */
public class PropertiesTest3 {
 
    @Test
    public void proRead(){
 
        ResourceBundle resourceBundle = ResourceBundle.getBundle("data");
        //遍历取值
        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();
            }
        }
 
    }
}



总结:

  • 第一种读取方式不需要引入第三方jar包,读取起来也很好理解。
  • 第二种方式需要引入Spring的核心依赖包,代码比较简单。
  • 第三种作为了解内容
  • properties文件的编码方式是GBK,我们读取文件的编码是ISO-8859-1,我们需要逆向编解码来处理乱码问题
 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值