This post will discuss how to write values to a properties file in Java.

1. In Plain Java

A properties file consists of key-value pairs of string type. We can write values to a properties file in plain Java using the Properties class.

The preferred way to save a properties list is to load the properties file from the classpath or file system into a Properties object and set the given property list (key and element pairs) in that object using its setProperty(…) method. Then we write the properties from this Properties object back to the properties file by passing a FileOutputStream via the store(OutputStream out, String comments) method of the Properties class. This is demonstrated below:

Download Code

2. Using Apache Commons Configuration

We can even write values to a properties file in Java using third-party libraries like Apache Commons Configuration. Here’s a working example:

Download Code

That’s all about writing values to a properties file in Java.