public class PropsUtil {
//Properties类读取*.properties属性文件,根据Key获取映射的Value
private static Properties properties = null;
static {
// 创建属性类对象
InputStream is = null;
properties = new Properties();
try {
// 加载外部*.properties文件到内存中
//D:\java82_jybc\Java_Reflect\src\main\resources\goods.properties
// 通过ClassLoader读取resources下的文件
is = PropsUtil.class.getClassLoader().getResourceAsStream("goods.properties");
properties.load(is);
} catch (IOException e) {
throw new RuntimeException(e);
}finally {
try {
is.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
public static String getProperties(String keyName){
return properties.getProperty(keyName);
}
public static void main(String[] args) {
System.out.println(PropsUtil.getProperties("className"));
}
}