This post shows how to update properties file in Java. As you know, Java Properties API doesn’t support update properties file dynamically. While properties files are a popular mean of configuring applications. Fortunately, Commons Configuration API supports this format and enhances significantly the basic Java Properties class. Here we will use Commons Configuration API to update properties file dynamically.
Let’s begin.
Create Mavan Project
In this how to update properties file in Java tutorial, we will create project with the final project structure directory like this:
Maven dependencies
We are using Commons Configuration API so that we need to add Commons Configuration dependency in pom.xml file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.javabycode</groupId> <artifactId>MavenJavaProject</artifactId> <version>1.0.0-SNAPSHOT</version> <name>JSON-EXAMPLE - ${project.artifactId}</name> <packaging>jar</packaging> <dependencies> <dependency> <groupId>commons-configuration</groupId> <artifactId>commons-configuration</artifactId> <version>1.10</version> </dependency> </dependencies> </project> |
Create properties file in classpath
Our properties file contains the content below
1 2 3 4 |
jdbc.driverClassName = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/javabycode jdbc.username = javabycode jdbc.password = mypassword |
Notice that the classpath is really src/main/resources directory. You can see this file in the project directory structure.
Create PropertiesUtil class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
package com.javabycode.properties; import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configuration.PropertiesConfiguration; public class PropertiesUtils { String fileName = "myconfig.properties"; public void updatePropValues() throws ConfigurationException { File propertiesFile = new File(getClass().getClassLoader().getResource(fileName).getFile()); PropertiesConfiguration config = new PropertiesConfiguration(propertiesFile); config.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect"); config.setProperty("hibernate.show_sql", "true"); config.setProperty("hibernate.format_sql", "true"); config.save(); } public void getPropValues() throws IOException { InputStream inputStream = null; try { Properties prop = new Properties(); inputStream = getClass().getClassLoader().getResourceAsStream(fileName); prop.load(inputStream); // get the property value and print it out System.out.println("jdbc.driverClassName=" + prop.getProperty("jdbc.driverClassName")); System.out.println("jdbc.url=" + prop.getProperty("jdbc.url")); System.out.println("jdbc.username=" + prop.getProperty("jdbc.username")); System.out.println("jdbc.password=" + prop.getProperty("jdbc.password")); System.out.println("hibernate.dialect=" + prop.getProperty("hibernate.dialect")); System.out.println("hibernate.show_sql=" + prop.getProperty("hibernate.show_sql")); System.out.println("hibernate.format_sql=" + prop.getProperty("hibernate.format_sql")); } catch (FileNotFoundException ex) { System.err.println("Property file '" + fileName + "' not found in the classpath"); ex.printStackTrace(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (Exception ex) { ex.printStackTrace(); } } } } } |
Create main class
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
package com.javabycode.properties; public class PropertiesFileExample { public static void main(String args[]) { try { PropertiesUtils proUtils = new PropertiesUtils(); //Update properties file dynamically proUtils.updatePropValues(); //Print all properties file proUtils.getPropValues(); } catch (Exception ex) { ex.printStackTrace(); } } } |
Run above main and we get the output like screen shot.
That’s all on the How to update properties file in Java tutorial.
Download complete source code by hitting the below link
UpdatePropertiesFile.zip (523 downloads)
Why did you not use latest commons-configuration2 dependency?
Please make a tutorial how to autoreload .properties at runtime without restarting server when they are changed in file system and not programmatically.
And how to read new values from reloaded file.