快捷獲取Properties中數據

寫了一個工具了,可以方便的訪問.properties文件中的數據,代碼如下

public final class MyProperties {
    private final static String[] PATHS = new String[]{"parameter.properties"};
    private Map<Object, Object> valueMap;
    private static volatile MyProperties myProperties;

    private MyProperties() {
        init();
    }

    private void init() {
        Map<Object, Object> currentMap = new HashMap<>(50);
        Arrays.asList(PATHS).forEach(x -> {
            try {
                InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(x);
                if (inputStream == null) {
                    throw new RuntimeException(String.format("路徑[%s]下的資源無法找到", x));
                }
                Properties properties = new Properties();
                properties.load(new InputStreamReader(inputStream));
                int originalSize = currentMap.size();
                currentMap.putAll(properties);
                //校驗值衝突
                if (originalSize + properties.size() > currentMap.size()) {
                    throw new RuntimeException("存在鍵衝突");
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        });
        valueMap = currentMap;
    }

    /**
     * 獲取字符串
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        Object o = valueMap.get(key);
        return o == null ? null : String.valueOf(o);
    }

    /**
     * 獲取字符串否則默認值
     *
     * @param key
     * @param defaultV
     * @return
     */
    public String getStringOrDefault(String key, String defaultV) {
        Object o = valueMap.get(key);
        return o == null ? defaultV : String.valueOf(o);
    }

    /**
     * 獲取integer
     *
     * @param key
     * @return
     */
    public Integer getInteger(String key) {
        Object o = valueMap.get(key);
        return o == null ? null : Integer.parseInt(String.valueOf(o));
    }

    /**
     * 獲取integer否則默認值
     *
     * @param key
     * @param defaultV
     * @return
     */
    public Integer getIntegerOrDefault(String key, Integer defaultV) {
        Object o = valueMap.get(key);
        return o == null ? defaultV : Integer.parseInt(String.valueOf(o));
    }

    /**
     * 刷新
     */
    public void refresh() {
        synchronized (this) {
            init();
        }
    }

    /**
     * 獲取對象
     *
     * @return
     */
    public static MyProperties getInstance() {
        if (myProperties == null) {
            synchronized (MyProperties.class) {
                if (myProperties == null) {
                    //這樣可以防止對象沒有初始化完成就被使用
                    MyProperties properties = new MyProperties();
                    myProperties = properties;
                }
            }
        }
        return myProperties;
    }
}

使用也很簡單,如下就可以讀取文件中的數據了

public class Main {
    public static void main(String[] args) {
        System.out.println(MyProperties.getInstance().getString("hello"));
        System.out.println(MyProperties.getInstance().getInteger("test"));
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章