Repository navigation
Expand file tree
/
Copy pathPropsUtil.java
More file actions
144 lines (132 loc) · 4 KB
/
Copy pathPropsUtil.java
File metadata and controls
144 lines (132 loc) · 4 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package bms.core.common.util;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* 属性文件操作工具类
*
* @author huangyong
* @since 1.0
*/
public class PropsUtil {
private static final Logger logger = LoggerFactory.getLogger(PropsUtil.class);
/**
* 加载属性文件
*/
public static Properties loadProps(String propsPath) {
Properties props = new Properties();
InputStream is = null;
try {
if (StringUtil.isEmpty(propsPath)) {
throw new IllegalArgumentException();
}
String suffix = ".properties";
if (propsPath.lastIndexOf(suffix) == -1) {
propsPath += suffix;
}
is = ClassUtil.getClassLoader().getResourceAsStream(propsPath);
if (is != null) {
props.load(is);
}
} catch (Exception e) {
logger.error("加载属性文件出错!", e);
throw new RuntimeException(e);
} finally {
try {
if (is != null) {
is.close();
}
} catch (IOException e) {
logger.error("释放资源出错!", e);
}
}
return props;
}
/**
* 加载属性文件,并转为 Map
*/
public static Map<String, String> loadPropsToMap(String propsPath) {
Map<String, String> map = new HashMap<String, String>();
Properties props = loadProps(propsPath);
for (String key : props.stringPropertyNames()) {
map.put(key, props.getProperty(key));
}
return map;
}
/**
* 获取字符型属性
*/
public static String getString(Properties props, String key) {
String value = "";
if (props.containsKey(key)) {
value = props.getProperty(key);
}
return value;
}
/**
* 获取字符型属性(带有默认值)
*/
public static String getString(Properties props, String key, String defalutValue) {
String value = defalutValue;
if (props.containsKey(key)) {
value = props.getProperty(key);
}
return value;
}
/**
* 获取数值型属性
*/
public static int getNumber(Properties props, String key) {
int value = 0;
if (props.containsKey(key)) {
value = CastUtil.castInt(props.getProperty(key));
}
return value;
}
// 获取数值型属性(带有默认值)
public static int getNumber(Properties props, String key, int defaultValue) {
int value = defaultValue;
if (props.containsKey(key)) {
value = CastUtil.castInt(props.getProperty(key));
}
return value;
}
/**
* 获取布尔型属性
*/
public static boolean getBoolean(Properties props, String key) {
return getBoolean(props, key, false);
}
/**
* 获取布尔型属性(带有默认值)
*/
public static boolean getBoolean(Properties props, String key, boolean defalutValue) {
boolean value = defalutValue;
if (props.containsKey(key)) {
value = CastUtil.castBoolean(props.getProperty(key));
}
return value;
}
/**
* 获取指定前缀的相关属性
*/
public static Map<String, Object> getMap(Properties props, String prefix) {
Map<String, Object> kvMap = new LinkedHashMap<String, Object>();
Set<String> keySet = props.stringPropertyNames();
if (CollectionUtil.isNotEmpty(keySet)) {
for (String key : keySet) {
if (key.startsWith(prefix)) {
String value = props.getProperty(key);
kvMap.put(key, value);
}
}
}
return kvMap;
}
}