使用commons-configuration读取properties配置文件

一、maven依耐:

<!-- https://mvnrepository.com/artifact/commons-configuration/commons-configuration -->
<dependency>
    <groupId>commons-configuration</groupId>
    <artifactId>commons-configuration</artifactId>
    <version>1.10</version>
</dependency>

二、工具类:

// 初始化map集合
    private static Map<String, PropertiesConfiguration> configMap = new HashMap<String, PropertiesConfiguration>();
    private static PropertiesConfiguration initConfigFile(String configFileName) throws ConfigurationException {
        PropertiesConfiguration propConfig = new PropertiesConfiguration();
        propConfig.setDelimiterParsingDisabled(true);       
        propConfig.setFileName(configFileName);
        propConfig.load();
        configMap.put(configFileName, propConfig);
        return propConfig;
    }

    /**
     * @Title: getProperty 
     * @Description: 根据文件路径及key值读取value
     * @param configFileName
     * @param key
     * @return
     */
    public static String getProperty(String configFileName, String key) {
        PropertiesConfiguration propConfig = configMap.get(configFileName);
        String value = null;
        try {
            if(propConfig == null) {
                propConfig = initConfigFile(configFileName);
            }
            if(propConfig.containsKey(key)) {
                Object propValue = propConfig.getProperty(key);
                value = (String)propValue;
            }           
        } catch(ConfigurationException ex) {
        }

        return value;       
    }

    /**
     * @Title: getProperties 
     * @Description: 根据文件路径读取配置文件的所有内容
     * @param configFileName
     * @return
     */
    @SuppressWarnings("rawtypes")
    public static Map<String, Object> getProperties(String configFileName) {
        PropertiesConfiguration propConfig = configMap.get(configFileName);
        try {
            if(propConfig == null) {
                propConfig = initConfigFile(configFileName);
            }
        } catch(ConfigurationException ex) {
            return null;
        }
        Map<String, Object> propMap = new HashMap<String, Object>();
        Iterator iter = propConfig.getKeys();
        while(iter.hasNext()) {
            String key = (String)iter.next();
            propMap.put(key, propConfig.getProperty(key));
        }
        return propMap;
    }


    /**
     * @Title: setProperty 
     * @Description: 保存key-value到配置文件中
     * @param configFileName
     * @param key
     * @param value
     */
    public static void setProperty(String configFileName, String key, String value) {
        PropertiesConfiguration propConfig = configMap.get(configFileName);     
        try {
            if(propConfig == null) {
                propConfig = initConfigFile(configFileName);
            }

            propConfig.setProperty(key, value);
            propConfig.save();
        } catch(ConfigurationException ex) {
        }
    }

    public static void main(String[] args) {
        Map<String, Object> props = PropertiesUtils.getProperties("D:\\aaa.properties");
        System.out.println(props);
        String value = PropertiesUtils.getProperty("D:\\aaa.properties","name");
        System.out.println(value);
    }

猜你喜欢

转载自blog.csdn.net/wgq3773/article/details/80777393
今日推荐