/**
* 读取配置文件的工具类
*
*/
public class ConfigManager {
// 第一步:构建私有的静态的实例
private static ConfigManager configManager;
private static Properties properties;
// 第二步:构造函数私有化
private ConfigManager() {
String configFile = "db.properties";
properties = new Properties();
InputStream in = ConfigManager.class.getClassLoader()
.getResourceAsStream(configFile);
try {
properties.load(in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 第三步:公开一个方法,返回单一实例
public static ConfigManager getInstance() {
if (configManager == null) {
configManager = new ConfigManager();
}
return configManager;
}
/**
* 根据key返回value
* @param key
* @return
*/
public String getString(String key) {
return properties.getProperty(key);
}
}