java 讀取設定檔(一)

來源:互聯網
上載者:User
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);    }}









聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.