-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPropertiesUtils.java
59 lines (50 loc) · 1.74 KB
/
PropertiesUtils.java
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package com.hp.gmall.realtime.utils;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
public class PropertiesUtils {
private static PropertiesUtils propertiesUtil = null;
private Properties properties = null;
private PropertiesUtils(String filePath) {
readPropertiesFile(filePath);
}
private void readPropertiesFile(String filePath) {
properties = new Properties();
InputStream in = null;
try {
in = this.getClass().getClassLoader().getResourceAsStream(filePath);
properties.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
assert in != null;
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static synchronized PropertiesUtils load(String filePath) {
if (propertiesUtil == null) {
propertiesUtil = new PropertiesUtils(filePath);
}
return propertiesUtil;
}
public Properties getProperties() {
return propertiesUtil.properties;
}
public int getInt(String key,int defaultValue) {
return Integer.parseInt(properties.getProperty(key, String.valueOf(defaultValue)));
}
public String getString(String key, String defaultValue) {
return properties.getProperty(key, defaultValue);
}
public Boolean getBoolean(String key, Boolean defaultValue) {
return Boolean.parseBoolean(properties.getProperty(key, String.valueOf(defaultValue)));
}
}