java update the configuration file properties

After experiencing today a demand to update the properties in the project configuration file, but the Internet to find a bunch of programs (java.util.properties and apache.commons.configuration.PropertiesConfiguration etc.) test encountered a common problem and can not be modified the stream file written to disk persistence. Finally found the problem lies in the path of the properties file, the relative path existing online sample code are used, such as: File file = new File ( "engine.properties"), here replaced by absolute path.

obtaining the absolute path java class as follows:

String filePath = yourClassName.class.getResource("/").getPath()+"engine.properties";

Returned the following results: C: \ apache-tomcat-7.0.68 \ webapps \ readydesk \ WEB-INF \ classes \

Wherein, getResource ( "/") returns the position of the classpath

getResource ( "") returns the class where the package name, such as: C: \ apache-tomcat-7.0.68 \ webapps \ readydesk \ WEB-INF \ classes \ com \ test \ impl \


Now given my code:

1. Based on java.util.properties

Without introducing additional advantage is that the jar package, but there is a clear disadvantage: in this way to modify the properties file, the order will be chaos its value.

String profilepath = PropertyUtil.class.getResource ( "/") getPath () + "config.properties";. // try my profile in the root directory {src

        Properties props=new Properties();

                props.load(new FileInputStream(profilepath));

                OutputStream fos =new FileOutputStream(profilepath);         

                props.setProperty ( "parameter", "parameter");

                props.store(fos, "Update value");

                fos.close();

            } catch (IOException e) {

            e.printStackTrace ();

                System.err.println ( "properties file update error");

            }

2.基于apache.commons.configuration.PropertiesConfiguration

In this way does not cause written order of key-value pairs disorder (configuration file which is highly recommended multi-parameter case), but it needs to introduce commons-configuration package (with the introduction of this package in the ConfigurationException throws an exception or error, error contents as follows: No exception of type ConfigurationException can be thrown; an exception type must be a subclass of Throwable; head given: The type

org.apache.commons.lang.exception.NestableException cannot be resolved.

It is indirectly referenced from required .class files

) In case of error, it is necessary to introduce commons-lang-2.6.jar version of the following

Sample code is as follows:

String profilepath = PropertyUtil.class.getResource("/").getPath()+filepath;

try {

PropertiesConfiguration config = new PropertiesConfiguration(profilepath);

  config.setAutoSave(true);

  config.setProperty(key, value);

                //System.out.println(config.getString(key));

} catch (ConfigurationException e) {

}

Guess you like

Origin blog.csdn.net/weixin_33924312/article/details/90830779