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

一、maven依耐:


<dependency>
    <groupId>commons-configurationgroupId>
    <artifactId>commons-configurationartifactId>
    <version>1.10version>
dependency>

二、工具类:

// 初始化map集合
    private static Map configMap = new HashMap();
    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 getProperties(String configFileName) {
        PropertiesConfiguration propConfig = configMap.get(configFileName);
        try {
            if(propConfig == null) {
                propConfig = initConfigFile(configFileName);
            }
        } catch(ConfigurationException ex) {
            return null;
        }
        Map propMap = new HashMap();
        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 props = PropertiesUtils.getProperties("D:\\aaa.properties");
        System.out.println(props);
        String value = PropertiesUtils.getProperty("D:\\aaa.properties","name");
        System.out.println(value);
    }

你可能感兴趣的:(JAVAEE)