• 一个读取propeties配置文件的工具类,线程安全的


    public class ConfigUtil {

    private static Map<String,Properties> map = new HashMap<String,Properties>();

    /**
    * 根据Properties文件名称获取Properties对象
    * @param name
    * @return Properties
    * @throws IOException
    */
    private synchronized static Properties createProperties(String name) throws IOException{
    Properties p = map.get(name);
    if(p == null){
    p = new Properties();
    p.load(ConfigUtil.class.getResourceAsStream(name));
    map.put(name, p);
    }
    return p;
    }
    /**
    * 根据Properties文件名和其中的key获取value
    * @param proName, key
    * @return String value
    * @throws IOException
    */
    public static String getValue(String proName,String key) throws IOException{
    return createProperties(proName).getProperty(key);
    }
    /**
    * 根据mqttconfig.properties文件的key获取value
    * @param String key
    * @return String value
    * @throws IOException
    */
    public static String getValue(String mqttKey) throws IOException{
    return createMQTTProperties().getProperty(mqttKey);
    }
    /**
    * 专门获取mqttconfig.properties的Properties
    * @param
    * @return Properties p
    * @throws IOException
    */
    public static Properties createMQTTProperties() throws IOException{
    Properties p = map.get("mqttconfig.properties");
    if(p == null){
    p = new Properties();
    p.load(ConfigUtil.class.getResourceAsStream("/mqttconfig.properties"));
    map.put("mqttconfig.properties", p);
    }
    return p;
    }
    public static void main(String[] args) throws IOException{
    System.out.println(getValue("/mqttconfig.properties","port"));
    }
    }

  • 相关阅读:
    发现一波黒帽seo神操作,通过百度打开跳广告,其他方式访问正常。下面分析原理。
    微信公众号js调起支付代码中不要使用var
    js 中 new Array() var a={} var a=[] new Object()
    Brute Force-python
    Cobaltstrike -恶意宏文件
    利用恶意office文档得shell
    nginx 安装配置php
    nginx 安装部署
    python 函数小实例
    三星s4刷机教程(卡刷)
  • 原文地址:https://www.cnblogs.com/zhaoblog/p/5948098.html
Copyright © 2020-2023  润新知