配置文件工具类

配置文件工具类

public class PropertiesHelper {

    private static final Logger logger = LoggerFactory.getLogger(PropertiesHelper.class);

    private Properties props;

    public PropertiesHelper(String fileName) {
        readPropertiesFile(fileName);
    }

    private void readPropertiesFile(String clazzPathFile) {
        props = new Properties();
        InputStreamReader is = null;
        try {
            is = new InputStreamReader(PropertiesHelper.class.getClassLoader().getResourceAsStream(clazzPathFile), "UTF-8");
            props.load(is);
        } catch (Exception e) {
            logger.error("Load properties file" + clazzPathFile + ", exception" + e.getMessage());
            new RuntimeException("Load properties file" + clazzPathFile, e);
        } finally {
            if (is != null)
                try {
                    is.close();
                } catch (IOException e) {
                    logger.info("Close file exception, " + e.getMessage());
                }
        }
    }

    /**
     * 根据key读取对应的value
     *
     * @param key
     * @return
     */
    public String get(String key) {
        return props.getProperty(key);
    }

    /**
     * 得到所有的配置信息
     *
     * @return
     */
    public Map<?, ?> getAll() {
        Map<String, String> map = new HashMap<String, String>();
        Enumeration<?> enu = props.propertyNames();
        while (enu.hasMoreElements()) {
            String key = (String) enu.nextElement();
            String value = props.getProperty(key);
            map.put(key, value);
        }
        return map;
    }
}

使用

private List<String> getAll() {
        PropertiesHelper propertiesHelper = new PropertiesHelper("config/buss.properties");
        String rs = propertiesHelper.get("ip.list");
        if (StringUtils.isNotBlank(rs)) {
            return Arrays.asList(rs.split(","));
        }
        return null;
    }

属性文件

ip.list=120.0.0.1,127.0.0.2
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 135,273评论 19 139
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 47,007评论 6 342
  • 国家电网公司企业标准(Q/GDW)- 面向对象的用电信息数据交换协议 - 报批稿:20170802 前言: 排版 ...
    庭说阅读 11,311评论 6 13
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 174,676评论 25 709
  • 一碗酒把兰州喝醉 想起你的眸子心就碎了 日子太懒 我们在风中凌乱 睡到日晒三杆 北方的大河穿过兰州 日头落过西山 ...
    艺术家清樾阅读 480评论 0 7