java中使用JDK的反射读取资源文件,下面代码粘贴即用,
public static String getValueByKey(String key){
InputStream inputStream = null;
Properties properties = null;
try {
properties = new Properties();
inputStream = LoadProperties.class.getResourceAsStream("/properties/systemProperty.properties");
properties.load(inputStream);
// 若存在该property,通过其key获取value,否则返回null
if(properties.containsKey(key)){
return properties.getProperty(key).trim();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
inputStream.close();
properties.clear();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
本文提供了一个Java实例,展示了如何利用JDK的反射API从资源目录加载和读取配置属性文件。
465

被折叠的 条评论
为什么被折叠?



