package com.trip.utils;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
//读取配置文件
public class ConfigManager {
private static ConfigManager configManager;
//根据属性文件中的键获得对应的值
public String getString(String key) {
String configFile="database.properties";
InputStream is=ConfigManager.class.getClassLoader().getResourceAsStream(configFile);
Properties properties=new Properties();
try {
properties.load(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return properties.getProperty(key);
}
//单实例方法
public static synchronized ConfigManager getConfigManager() {
if(configManager==null) {
configManager=new ConfigManager();
}
return configManager;
}
}
|
|