How to update properties file in Java

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:

How to update properties file in Java

Maven dependencies
We are using Commons Configuration API so that we need to add Commons Configuration dependency in pom.xml file

Create properties file in classpath

Our properties file contains the content below

Notice that the classpath is really src/main/resources directory. You can see this file in the project directory structure.

Create PropertiesUtil class

Create main class

Run above main and we get the output like screen shot.

updatepropertiesfile-output

That’s all on the How to update properties file in Java tutorial.

Download complete source code by hitting the below link

UpdatePropertiesFile.zip (61 downloads)


2 thoughts on “How to update properties file in Java

  1. 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.

Leave a Comment

*

Please share it if you found this useful
Hide Buttons