Java實現的properties文件動態(tài)修改并自動保存工具類
本文實例講述了Java實現的properties文件動態(tài)修改并自動保存工具類。分享給大家供大家參考,具體如下:
一、概述
利用commons-configuration讀取配置文件,并實現對配置文件的動態(tài)修改和自動保存。
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) { //執(zhí)行初始化 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包本站下載地址如下:
更多關于java算法相關內容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數據結構與算法教程》、《Java操作DOM節(jié)點技巧總結》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設計有所幫助。
相關文章
Java中的ScheduledThreadPoolExecutor定時任務詳解
這篇文章主要介紹了Java中的ScheduledThreadPoolExecutor詳解,??ScheduledThreadPoolExecutor?繼承自?ThreadPoolExecutor,它主要用來在給定的延遲之后運行任務,或者定期執(zhí)行任務,ScheduledThreadPoolExecutor?的功能與?Timer?類似<BR>,需要的朋友可以參考下2023-12-12