private static Properties config = null;
static {
InputStream in = PropertiesUtils.class.getClassLoader()
.getResourceAsStream("config.properties");
config = new Properties();
try {
config.load(in);
//in.close();
} catch (IOException e) {
System.out.println("No config.properties defined error");
}finally{
if(null != in){
try {
in.close();
} catch (IOException e) {
System.out.println("InputStream close error!");
}
}
}
}
// 根据key读取value
public static String readValue(String key) {
// Properties props = new Properties();
try {
String value = config.getProperty(key);
return value;
} catch (Exception e) {
e.printStackTrace();
System.err.println("ConfigInfoError" + e.toString());
return null;
}
}
// 读取properties的全部信息
public static void readAllProperties() {
try {
Enumeration en = config.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
String Property = config.getProperty(key);
System.out.println(key + Property);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("ConfigInfoError" + e.toString());
}
}
properties读取函数
最新推荐文章于 2025-06-01 09:09:09 发布