Java中读取properties配置文件的八种方式总结
# Java中读取properties配置文件的八种方式总结
提示
摘自:
https://blog.csdn.net/Thinkingcao/article/details/111897862
Properties类
Properties 类位于 java.util.Properties中,是Java 语言的处理配置文件所使用的类,其中的xxx.Properties类主要用于集中的持久存储Java的配置文件内容,可以读取后缀是.properties和.cfg的配置文件。
Properties继承了Hashtable 类,以Map 的形式进行放置值,put(key,value) 和 get(key),文本注释信息可以用"#"来注释。
Properties 类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。属性列表中每个键及其对应值都是一个字符串。
Properties 文件内容的格式是:键=值 形式,Key值不能够重复。 例如:
jdbc.driver=com.mysql.jdbc.Driver
1
Properties类的继承关系图:
# 1. 从当前的类加载器的getResourcesAsStream来获取
/**
* 1. 方式一
* 从当前的类加载器的getResourcesAsStream来获取
* InputStream inputStream = this.getClass().getResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test1() throws IOException {
InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("jdbc.url");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 2. 从当前的类加载器的getClassLoader().getResourcesAsStream来获取
/**
* 2. 方式二
* 从当前的类加载器的getResourcesAsStream来获取
* InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test2() throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 3. 使用Class类的getSystemResourceAsStream静态方法 和使用当前类的ClassLoader是一样的
/**
* 3. 方式三
* 使用Class类的getSystemResourceAsStream方法 和使用当前类的ClassLoader是一样的
* InputStream inputStream = ClassLoader.getSystemResourceAsStream(name)
*
* @throws IOException
*/
@Test
public void test3() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 4. 使用Spring-core包中的ClassPathResource读取
/**
* 4. 方式四
* Resource resource = new ClassPathResource(path)
*
* @throws IOException
*/
@Test
public void test4() throws IOException {
Resource resource = new ClassPathResource("config/application.properties");
Properties properties = PropertiesLoaderUtils.loadProperties(resource);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 5. 从文件中读取,new BufferedInputStream(InputStream in)
/**
* 5. 方式五
* 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
* BufferedInputStream继承自InputStream
* InputStream inputStream = new BufferedInputStream(new FileInputStream(name)
* 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
* @throws IOException
*/
@Test
public void test5() throws IOException {
InputStream inputStream = new BufferedInputStream(new FileInputStream("src/main/resources/config/application.properties"));
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 6.从文件中读取,new FileInputStream(String name)
/**
* 6. 方式六
* 从文件中获取,使用InputStream字节,主要是需要加上当前配置文件所在的项目src目录地址。路径配置需要精确到绝对地址级别
* FileInputStream继承自InputStream
* InputStream inputStream = new FileInputStream(name)
* 这种方法读取需要完整的路径,优点是可以读取任意路径下的文件,缺点是不太灵活
* @throws IOException
*/
@Test
public void test6() throws IOException {
InputStream inputStream = new FileInputStream("src/main/resources/config/application.properties");
Properties properties = new Properties();
properties.load(inputStream);
properties.list(System.out);
System.out.println("==============================================");
String property = properties.getProperty("minio.endpoint");
System.out.println("property = " + property);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 7. 使用PropertyResourceBundle读取InputStream流
/**
* 7. 方式七
* 使用InputStream流来进行操作ResourceBundle,获取流的方式由以上几种。
* ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
* @throws IOException
*/
@Test
public void test7() throws IOException {
InputStream inputStream = ClassLoader.getSystemResourceAsStream("config/application.properties");
ResourceBundle resourceBundle = new PropertyResourceBundle(inputStream);
Enumeration<String> keys = resourceBundle.getKeys();
while (keys.hasMoreElements()) {
String s = keys.nextElement();
System.out.println(s + " = " + resourceBundle.getString(s));
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 8. 使用ResourceBundle.getBundle读取
/**
* 8 .方式八
* ResourceBundle.getBundle的路径访问和 Class.getClassLoader.getResourceAsStream类似,默认从根目录下读取,也可以读取 resources目录下的文件
* ResourceBundle rb = ResourceBundle.getBundle("b") //不需要指定文件名的后缀,只需要写文件名前缀即可
*/
@Test
public void test8(){
//ResourceBundle rb = ResourceBundle.getBundle("jdbc"); //读取resources目录下的jdbc.properties
ResourceBundle rb2 = ResourceBundle.getBundle("config/application");//读取resources/config目录下的application.properties
for(String key : rb2.keySet()){
String value = rb2.getString(key);
System.out.println(key + ":" + value);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
上次更新: 2023/09/05 17:45:42