A properties file consists of a set of key-value pairs. This article covers different ways to read or write values to the properties file in Kotlin.

1. Write values to properties file

The preferred way to write values to a properties file is to load it first from the classpath or file system into a Properties object and set the given property using its setProperty() function. Then write the properties to the properties file by passing a FileOutputStream to the store() function.

Download Code

2. Read values from properties file

The stringPropertyNames() function returns collection of keys present in the Properties object. The following example loads the current system properties into a Properties object using the System.getProperties() and then list out all system properties using the stringPropertyNames() function.

Download Code

 
Here’s how you can load properties from the property file present on the file system.

Download Code

That’s all about reading and writing values to the properties file in Kotlin.