近日有个需求需要静态方法获取properties,在此记录下
public class TestDemo {
private static Map<String, String> readPropToMap(String propertiesPath) {
Map<String, String> map = new HashMap(16);
try {
InputStream is = TestDemo.class.getClassLoader().getResourceAsStream(propertiesPath);
if (is == null) {
return null;
} else {
Properties prop = new Properties();
prop.load(is);
Iterator var4 = prop.stringPropertyNames().iterator();
while(var4.hasNext()) {
String key = (String)var4.next();
map.put(key, prop.getProperty(key));
}
return map;
}
} catch (IOException var6) {
throw (new SaTokenException("配置文件(" + propertiesPath + ")加载失败", var6)).setCode(10021);
}
}
public static void main(String[] args) {
Map<String, String> stringStringMap = readPropToMap("application.properties");
System.out.println(stringStringMap);
}
}