Saturday, 24 October 2020

Update a property value without deleting other properties in properties file Using Apache Commons Configuration Java

PropertiesConfiguration class of Apache Commons Configuration 1.10 provides save method which lets you save the updated properties file without deleting the previous values.

In this case the Properties file is present outside of the Jar gets modified by the Jar.


public static void setProperty(String property, String value) {

PropertiesConfiguration conf = null;

try {

conf = new PropertiesConfiguration("./config.properties");

} catch (ConfigurationException e) {

e.printStackTrace();

}

conf.setProperty(property, value);

try {

conf.save();

} catch (ConfigurationException e) {

e.printStackTrace();

}

}