下面是java properties读取文件的几种方式
代码:1
2
3
4
5
6
7
8
9
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
35
36
37
38
39
40
41
42
43/*
* java properties读取文件的几种方式
* */
public class PropertiesTest {
// 1.通过本类的类加载器的getResourceAsStream获取
public Properties getByClassLoaderResourceAsStream(String filePath) throws IOException {
Properties properties = new Properties();
properties.load(new InputStreamReader(PropertiesTest.class.getClassLoader().getResourceAsStream(filePath)));
return properties;
}
// 2.通过文件流进行读取
public Properties getByFileInputStream(String filePath) throws IOException {
Properties properties = new Properties();
properties.load(new FileInputStream(new File(filePath)));
return properties;
}
// 3.通过类加载器ClassLoader获取
public Properties getByClassLoader(String filePath) throws IOException {
Properties properties = new Properties();
properties.load(ClassLoader.getSystemResourceAsStream(filePath));
return properties;
}
// 4.通过URL来获取
public Properties getByURL(String url) throws IOException {
Properties properties = new Properties();
properties.load(new URL(url).openStream());
return properties;
}
// 使用
public static void main(String[] args) {
Properties properties = new Properties();
String value = properties.getProperty("key");
if(StringUtils.isEmpty(value)) {
}
}
}