private static Properties props =
new Properties();
static {
try {
props.load(PropertiesUtil.class
.getClassLoader()
.getResourceAsStream(
"opt.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key){
String val = props.getProperty(key);
return val;
}
new Properties();
static {
try {
props.load(PropertiesUtil.class
.getClassLoader()
.getResourceAsStream(
"opt.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String getValue(String key){
String val = props.getProperty(key);
return val;
}
本文介绍了一种使用Java静态代码实现加载配置文件属性的方法,通过Properties类和类加载器获取资源流来读取opt.properties文件的内容,并提供了一个简单易用的接口getValue()来获取指定键对应的值。
1908

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



