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 PropertiesConfiguration 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
package crunchify.com.tutorial; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; /** * @author Crunchify.com * */ 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 config = new PropertiesConfiguration("/Users/<username>/Documents/config.properties"); config.setProperty("company1", "Crunchify"); config.setProperty("company2", "Google"); config.setProperty("Crunchify_Address", "NYC, US"); config.setProperty("Google_Address", "Mountain View, CA, US"); config.save(); System.out.println("Config Property Successfully Updated.."); } }
Other must read: http://crunchify.com/java-properties-file-how-to-read-config-properties-values-in-java/
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:
The post Java Properties Files: How to Update config.properties File in Java? appeared first on Crunchify.
Author: App Shah