Java uses a commons-configuration.jar to dynamically modify the configuration file (. properties)

Source: Internet
Author: User
Import Java. util. map; import Org. apache. commons. configuration. configurationexception; import Org. apache. commons. configuration. propertiesconfiguration; import Org. apache. commons. configuration. reloading. filechangedreloadingstrategy; import Org. apache. log4j. logger; /***** configuration file properties automatically loaded class * @ author lyh * @ version 2012-6-5 * @ see propertiesautoload * @ since */public class propertiesautoload {/*** log */private static final logger log = logger. getlogger (propertiesautoload. class);/*** Singleton */Private Static final propertiesautoload auto_load = new propertiesautoload ();/*** configuration */Private Static propertiesconfiguration propconfig; /*** automatically save */Private Static Boolean autosave = true; /* ** properties file path * @ Param propertiesfile * @ return * @ see */public static propertiesautoload getinstance (string propertiesfile) {// execute initialization Init (propertiesfile ); return auto_load;}/*** obtain the corresponding value based on the key * @ Param key * @ return * @ see */public object getvaluefrompropfile (string key) {return propconfig. getproperty (key);}/*** get the corresponding value array * @ Param key * @ return * @ see */Public String [] getarrayfrompropfile (string key) {return propconfig. getstringarray (key);}/*** set attribute * @ Param key * @ Param value * @ see */Public void setproperty (string key, string value) {propconfig. setproperty (Key, value);}/*** set attribute * @ Param map * @ see */Public void setproperty (Map <string, string> map) {for (string key: map. keyset () {propconfig. setproperty (Key, map. get (key) ;}}/*** constructor privatize */private propertiesautoload () {}/*** initialize * @ Param propertiesfile * @ see */Private Static void Init (string propertiesfile) {try {propconfig = new propertiesconfiguration (propertiesfile ); // automatically reload propconfig. setreloadingstrategy (New filechangedreloadingstrategy (); // automatically save propconfig. setautosave (autosave);} catch (configurationexception e) {log. error (E. getmessage () ;}}/*** test * @ Param ARGs * @ see */public static void main (string [] ARGs) {log.info (propertiesautoload. getinstance ("props/config/xx. properties "). getvaluefrompropfile ("pagesize "));}}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.