Properties files are a popular mean of configuring applications. Of course Commons Configuration supports this format and enhances significantly the basic java.util.Properties
class.
This section introduces the features of the PropertiesConfiguration
class. Note that Properties Configuration is a very typical example for an implementation of the Configuration interface and many of the features described in this section (e.g. list handling or interpolation) are supported by other configuration classes as well.
This is because most configuration implementations that ship with Commons Configuration are derived from the common base class AbstractConfiguration
, which implements these features.
Below is a simple code which will help you update
config.properties file via java. For reading properties file
follow this tutorial.
You need this .jar files in your class path: commons-configuration-1.9.jar
.
OR
If you have maven project then use below dependency:
<dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency>
CrunchifyUpdateConfig.java
Here is a simple program to update config.properties file.
package crunchify.com.tutorials; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; /** * @author Crunchify.com * Java Properties Files: How to Update config.properties File in Java? */ public class CrunchifyUpdateConfig { public static void main(String[] args) throws ConfigurationException { // You have to create config.properties file under resources folder or anywhere you want :) // Here I'm updating file which is already exist under /Documents // PropertiesConfiguration: This is the "classic" Properties loader which loads the values from a single or multiple files (which can be chained with "include =". // All given path references are either absolute or relative to the file name supplied in the constructor. // In this class, empty PropertyConfigurations can be built, properties added and later saved. // Include statements are (obviously) not supported if you don't construct a PropertyConfiguration from a file. // The properties file syntax is explained here, // basically it follows the syntax of the stream parsed by java.util.Properties.load and adds several useful extensions PropertiesConfiguration config = new PropertiesConfiguration("/Users/app/Documents/config.properties"); // setProperty(): Sets a new value for the specified property. // This implementation checks if the auto save mode is enabled and saves the configuration if necessary. config.setProperty("company1", "Crunchify"); config.setProperty("company2", "Google"); config.setProperty("Crunchify_Address", "NYC, US"); config.setProperty("Google_Address", "Mountain View, CA, US"); // save(): Save the configuration. Before this method can be called a valid file name must have been set. config.save(); System.out.println("Config Property Successfully Updated.."); } }
Other must read: https://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
Console Result:
Just run above program as a Java Application and you should see result as below.
log4j:WARN No appenders could be found for logger (org.apache.commons.configuration.PropertiesConfiguration). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info. Config Property Successfully Updated..
config.properties file content:
Just to to Downloads folder and you should see file with below contents.
Let us know if you face any issue running above program.