java update properties file run time

The Java Properties class (api here) specifies "load" and "store" methods which should do exactly that. Use FileInputStream and FileOutputStream to specify the file to save it into.


I completely agree that Apache Commons Configuration API is really good choice.

This example update properties at runtime

File propertiesFile = new File(getClass().getClassLoader().getResource(fileName).getFile());        
PropertiesConfiguration config = new PropertiesConfiguration(propertiesFile);           
config.setProperty("hibernate.show_sql", "true");
config.save();

From the post how to update properties file in Java

Hope this help!


You could use a very simple approach based on the java.util.Properties class which has indeed a load and store methods that you can use in conjunction with a FileInputStream and FileOutputStream:

But actually, I'd recommend to use an existing configuration library like Commons Configuration (amongst others). Check the Properties Howto to see how to load, save and automatically reload a properties file using its API.

Tags:

Java