• 配置文件读取类



    定义

    
    package com.wp.tool;
    
    import java.util.Date;
    import java.util.HashMap;
    import java.util.MissingResourceException;
    import java.util.ResourceBundle;
    
    /**
     * 资源文件读取工具
     * @author pengwu7
     * @date 2017年9月7日
     */
    public class PropertiesFileUtil {
    
        // 当打开多个资源文件时,缓存资源文件
        private static HashMap<String, PropertiesFileUtil> configMap = new HashMap<String, PropertiesFileUtil>();
        // 打开文件时间,判断超时使用
        private Date loadTime = null;
        // 资源文件
        private ResourceBundle resourceBundle = null;
        // 默认资源文件名称
        private static final String NAME = "config";
        // 缓存时间
        private static final Integer TIME_OUT = 60 * 1000;
    
        // 私有构造方法,创建单例
        private PropertiesFileUtil(String name) {
            this.loadTime = new Date();
            this.resourceBundle = ResourceBundle.getBundle(name);
        }
    
        public static synchronized PropertiesFileUtil getInstance() {
            return getInstance(NAME);
        }
    
        public static synchronized PropertiesFileUtil getInstance(String name) {
            PropertiesFileUtil conf = configMap.get(name);
            if (null == conf) {
                conf = new PropertiesFileUtil(name);
                configMap.put(name, conf);
            }
            // 判断是否打开的资源文件是否超时1分钟
            if ((new Date().getTime() - conf.getLoadTime().getTime()) > TIME_OUT) {
                conf = new PropertiesFileUtil(name);
                configMap.put(name, conf);
            }
            return conf;
        }
    
        // 根据key读取value
        public String get(String key) {
            try {
                String value = resourceBundle.getString(key);
                return value;
            }catch (MissingResourceException e) {
                return "";
            }
        }
    
        // 根据key读取value(整形)
        public Integer getInt(String key) {
            try {
                String value = resourceBundle.getString(key);
                return Integer.parseInt(value);
            }catch (MissingResourceException e) {
                return null;
            }
        }
    
        // 根据key读取value(布尔)
        public boolean getBool(String key) {
            try {
                String value = resourceBundle.getString(key);
                if ("true".equals(value)) {
                    return true;
                }
                return false;
            }catch (MissingResourceException e) {
                return false;
            }
        }
    
        public Date getLoadTime() {
            return loadTime;
        }
    
    }
    
    


    使用

    
    PropertiesFileUtil.getInstance("config").get("xxx");
    PropertiesFileUtil.getInstance().get("xxx");
    
    
  • 相关阅读:
    路飞学城Python-Day4(practise)
    事件绑定和普通事件的区别
    数组中shift(),push(),unshift(),pop()方法
    IE6中常见BUG与相应的解决办法
    apply()与call()的区别
    CSS引入的方式有哪些? link和@import的区别是?
    substring() , slice() and substr()方法
    webpack常用命令总结
    webpack
    从输入URL 到页面加载完成的过程
  • 原文地址:https://www.cnblogs.com/qixidi/p/10221711.html
Copyright © 2020-2023  润新知