简单的读取系统属性配置文件的工具类

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;

/**
* 读取系统属性配置文件的工具类
*/
public class SystemConfig {
// 私有锁
private final static Object lock = new Object();

private static String FILE_CONFIG_DIR = System.getProperty("user.dir") + File.separator;

/** 数据库的配置文件名称 */
private static final String DATABASE_CONFIG_FILE_NAME_Oracle = "ODBConfig.properties";

/** 该对象的一个实例 */
private static SystemConfig instance;

/** Oracle数据库配置文件属性集*/
private Properties databaseConfigProOracle;

private static final Logger log = Logger.getLogger(SystemConfig.class);

/**
* 默认构造函数
*/
private SystemConfig() {
// 加载数据库的相关配置
databaseConfigProOracle = loadConfig(FILE_CONFIG_DIR + DATABASE_CONFIG_FILE_NAME_Oracle);
}

/**
* 取得该对象的一个实例
* @return ImportConfig 该对象的一个实例
*/
public static SystemConfig getInstance() {
if (null == instance) {
synchronized (lock) {
if (null == instance) {
instance = new SystemConfig();
}
}
}
return instance;
}

/**
* 加载系统配置文件
* @param configFilePath
* @return Properties [返回类型说明]
*/
public static Properties loadConfig(String configFilePath) {
Properties configPro = new Properties();
FileInputStream fileInputStream = null;
try {
File file=FileUtils.getFile(configFilePath);
if(null==file)
{
throw new Exception();
}
fileInputStream = new FileInputStream(file);
configPro.load(fileInputStream);
} catch (Exception e) {
log.error(e);
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
fileInputStream = null;
} catch (IOException ioe) {
log.error(ioe);
}
}
}
return configPro;
}

/**
* 取得Oracle数据库的配置文件属性集
* @return Properties 数据库的配置文件属性集
*/
public Properties getDatabaseConfigProOracle() {
return databaseConfigProOracle;
}

}

posted on 2019-03-21 17:54  吴胖子哦  阅读(314)  评论(0编辑  收藏  举报

导航