properties文件动态修改并自动保存

来源:互联网 发布:linux windows samba 编辑:程序博客网 时间:2024/05/22 09:17

利用commons-configuration读取配置文件,并实现对配置文件的动态修改和自动保存。

public class Config {    private static PropertiesConfiguration propConfig;    private static final Config CONFIG = new Config();    /**     * 自动保存     */    private static boolean autoSave = true;    private Config() {    }    public static Config getInstance(String propertiesFile) {        //执行初始化         init(propertiesFile);        return CONFIG;    }    /**     * 初始化     *     * @param propertiesFile     * @see     */    private static void init(String propertiesFile) {        try {            propConfig = new PropertiesConfiguration(propertiesFile);            //自动重新加载             propConfig.setReloadingStrategy(new FileChangedReloadingStrategy());            //自动保存             propConfig.setAutoSave(autoSave);        } catch (ConfigurationException e) {            e.printStackTrace();        }    }    /**     * 根据Key获得对应的value     *     * @param key     * @return     * @see     */    public Object getValue(String key) {        return propConfig.getProperty(key);    }    /**     * 设置属性     *     * @param key     * @param value     * @see     */    public void setProperty(String key, String value) {        propConfig.setProperty(key, value);    }}

转自:http://huangke.info/propertieswen-jian-dong-tai-xiu-gai-bing-zi-dong-bao-cun/

1 1
原创粉丝点击