package com.xxx.config;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.util.Properties;import org.apache.log4j.Logger;/** * 讀配置整合類<br> * @author */public class Configs { static Logger log = Logger.getLogger(Configs.class.getName()); private Properties props = null; protected void init(String file) { this.init(file, props); } protected void init(String file, Properties defaul) { File f = new File(file); if (f.exists()) { FileInputStream fis = null; try { fis = new FileInputStream(f); } catch(FileNotFoundException e2) { e2.printStackTrace(); throw new ConfigException(e2); } props = new Properties(defaul); try { props.load(fis); fis.close(); } catch(IOException e) { e.printStackTrace(); throw new ConfigException(e); } } else { System.out.println(file + "設定檔沒有找到!"); throw new ConfigException(file + "設定檔沒有找到!"); } } /** * 得到包含配置資訊的Properties對象 * @return Properties對象 */ public Properties getConfigProperty() { return props; } /** * 根據配置節點名得到配置值 * @param key 配置節點名 * @param defaul 預設值 * @param islog 是否寫日誌。如果為true且返回的配置值為null,則寫日誌 * @return 如果有配置值,則返回配置值,否則返回預設值 */ public String getConfig(String key, String defaul, boolean isLog) { String keyVal = props.getProperty(key); if (keyVal == null && isLog) { log.error("設定檔中沒有名為:" + key + "的KEY值!"); } if (keyVal == null) { keyVal = defaul; } if (keyVal != null && keyVal.endsWith("\"")) keyVal = keyVal.replaceAll("\"", ""); return keyVal; } /** * 根據配置節點名得到配置值 * @param key 配置節點名 * @param defaul 預設值 * @return 如果有配置值,則返回配置值,否則返回預設值 */ public String getConfig(String key, String defaul) { return getConfig(key, defaul, false); } /** * 根據配置節點名得到配置值 * @param key 配置節點名 * @return 配置值 */ public String getConfig(String key) { return getConfig(key, null, false); } /** * 根據配置節點名得到配置值 * @param key 配置節點名 * @param islog 是否寫日誌。如果為true且返回的配置值為null,則寫日誌 * @return 配置值 */ public String getConfig(String key, boolean islog) { return getConfig(key, null, islog); }}