在开发过程中常常会需要读取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,我们需要逆向编解码来处理乱码问题