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;
}