使用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);
    }
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值