首页 > 编程 > Java > 正文

Java实现的properties文件动态修改并自动保存工具类

2019-11-26 11:00:39
字体:
来源:转载
供稿:网友

本文实例讲述了Java实现的properties文件动态修改并自动保存工具类。分享给大家供大家参考,具体如下:

一、概述

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

Apache Common-Configuration工具可以从
Properties文件,XML文件,JNDI,JDBC数据源,System Properties,Applet parameters,Servlet Parameters等读取相应信息

使用步骤

前提,引入commons-configuration-1.6.jar这个JAR包,同时还必须映入commm-logging.jar,common-lang.jar和common-collection.jar

二、示例:

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);  }}

附:相关jar包本站下载地址如下:

commons-configuration-1.6.jar

commm-logging.jar

common-lang.jar

common-collection.jar

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表