package com.thunisoft.config;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.log4j.Logger;
public class ConfigManager {
private static Logger logger = Logger.getLogger(ConfigManager.class);
private String configFileName;
/**
* 读取配置文件构造方法
* @param configFileName 配置文件的名字
* */
public ConfigManager(String configFileName) {
logger.info("加载配置文件:"+configFileName);
this.configFileName = configFileName;
}
/**
* 读取配置文件中的value
* @param key 配置文件中的key
* */
public String getValue(String key) {
String proFilePath = System.getProperty("user.dir") + "/"
+ configFileName;
InputStream in;
ResourceBundle resourceBundle;
String value = null;
try {
in = new BufferedInputStream(new FileInputStream(proFilePath));
resourceBundle = new PropertyResourceBundle(in);
value = new String(resourceBundle.getString(key).getBytes(
"ISO-8859-1"), "UTF-8");
} catch (FileNotFoundException e) {
logger.error("配置文件"+configFileName+"不存在,请检查");
logger.error(e.getMessage());
} catch (IOException e) {
logger.error("读取配置文件"+configFileName+"失败");
logger.error(e.getMessage());
}
return value;
}
}