java讀取配置文件工具

1,加載配置文件

public class PropertiesLoader {

    private static Logger logger = Logger.getLogger(PropertiesLoader.class);

    private static ResourceLoader resourceLoader = new DefaultResourceLoader();

    private final Properties properties;

    public PropertiesLoader(String... resourcesPaths) {
        properties = loadProperties(resourcesPaths);
    }

    public Properties getProperties() {
        return properties;
    }

    /**
     * 取出Property,但以System的Property優(yōu)先,取不到返回空字符串.
     */
    private String getValue(String key) {
        String systemProperty = System.getProperty(key);
        if (systemProperty != null) {
            return systemProperty;
        }
        if (properties.containsKey(key)) {
            return properties.getProperty(key);
        }
        return "";
    }

    /**
     * 取出String類型的Property,但以System的Property優(yōu)先,如果都為Null則拋出異常.
     */
    public String getProperty(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return value;
    }

    /**
     * 取出String類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值.
     */
    public String getProperty(String key, String defaultValue) {
        String value = getValue(key);
        return value != null ? value : defaultValue;
    }

    /**
     * 取出Integer類型的Property,但以System的Property優(yōu)先.如果都為Null或內(nèi)容錯誤則拋出異常.
     */
    public Integer getInteger(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Integer.valueOf(value);
    }

    /**
     * 取出Integer類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值,如果內(nèi)容錯誤則拋出異常
     */
    public Integer getInteger(String key, Integer defaultValue) {
        String value = getValue(key);
        return value != null ? Integer.valueOf(value) : defaultValue;
    }

    /**
     * 取出Double類型的Property,但以System的Property優(yōu)先.如果都為Null或內(nèi)容錯誤則拋出異常.
     */
    public Double getDouble(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Double.valueOf(value);
    }

    /**
     * 取出Double類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值,如果內(nèi)容錯誤則拋出異常
     */
    public Double getDouble(String key, Integer defaultValue) {
        String value = getValue(key);
        return value != null ? Double.valueOf(value) : defaultValue;
    }

    /**
     * 取出Boolean類型的Property,但以System的Property優(yōu)先.如果都為Null拋出異常,如果內(nèi)容不是true/false則返回false.
     */
    public Boolean getBoolean(String key) {
        String value = getValue(key);
        if (value == null) {
            throw new NoSuchElementException();
        }
        return Boolean.valueOf(value);
    }

    /**
     * 取出Boolean類型的Property,但以System的Property優(yōu)先.如果都為Null則返回Default值,如果內(nèi)容不為true/false則返回false.
     */
    public Boolean getBoolean(String key, boolean defaultValue) {
        String value = getValue(key);
        return value != null ? Boolean.valueOf(value) : defaultValue;
    }

    /**
     * 載入多個文件, 文件路徑使用Spring Resource格式.
     */
    private Properties loadProperties(String... resourcesPaths) {
        Properties props = new Properties();
        for (String location : resourcesPaths) {
            InputStream is = null;
            try {
                Resource resource = resourceLoader.getResource(location);
                is = resource.getInputStream();
                props.load(is);
            } catch (IOException ex) {
                logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
            } finally {
                IOUtils.closeQuietly(is);
            }
        }
        return props;
    }

2.獲取配置

public final class Global {

    private Global() {
    }

    public static Global getInstance() {
        return LazyHolder.GLOBAL;
    }

    private static class LazyHolder {
        private static final Global GLOBAL = new Global();
    }

    /**
     * 保存全局屬性值
     */
    private static Map<String, String> map = Maps.newConcurrentMap();

    public static final String DEFAULT_CHARTSET = "UTF-8";


    /**
     * 屬性文件加載對象
     */
    private static PropertiesLoader loader = new PropertiesLoader("配置文件名稱");

    /**
     * 獲取配置
     */
    public static String getConfig(String key) {
        String value = map.get(key);
        if (value == null) {
            value = loader.getProperty(key);
            map.put(key, value != null ? value : StringUtils.EMPTY);
        }
        return value;
    }
}
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,554評論 19 139
  • Spring Boot 參考指南 介紹 轉載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,273評論 6 342
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,030評論 25 709
  • 人的旅途就像一列開往世界盡頭的列車,人來人往,熙熙攘攘。在成長的路上,我們會得到很多東西:那些新奇的世界和未知的未...
    Lu羊羊閱讀 868評論 1 2
  • 自制酸奶失敗的原因? 1、選用的菌粉失效:菌粉失效后,失去活性,影響發(fā)酵成功率??稍谑褂们鞍丫廴鲈谝粡埌准埳?,正...
    易麻麻閱讀 1,403評論 0 0

友情鏈接更多精彩內(nèi)容