Properties files

Properties files stores simple parameters as key-value pairs, outside of compiled code. Key and Values are separated using '=' sign

Most of the time properties file used to store URL, username, password, environment, etc..

Eg:Key=Value Desert=hot

Read Properties File

We can read and write the properties file in Kotlin using the Properties class present in the Kotlin, this class provides few methods to read and write the data into the properties file.

Insights about properties files

  • You can store the file anywhere in the local file system, better if you can place it relative to your project
  • Recommended .properties extension however you can choose your own extension like text so on.
  • Properties class extends Hash table, you can iterate over the values in properties file like you would in a Hashtable.

Follow the below steps to read property file
1. Create a Kotlin file in IntelliJ
2. Create a properties file on your local machine with .properties extension.

properties-file-kotlin

3. Create Object for FileInputStream with the properties file path, it makes the file into a stream of input item

val fis = FileInputStream("C:pathconfig.properties")

4. Create a Properties class object to access the property file.

val prop = Properties()

5. Load the property file using load() present in the Properties object

prop.load(fis);

6. Get the values using 'get("key")' method or getProperty("key") method by passing key as the parameter

println(prop.getProperty("url"))

Complete Program to Read a value from a properties file with k

import java.io.FileInputStream
import java.util.*
fun main(args: Array<String>) {
    // create file input stream object for the properties file
    val fis = FileInputStream("C:pathconfig.properties")
    // create Properties class object to access properties file
    val prop = Properties()
    // load the properties file
    prop.load(fis)
    // get the property of "url" using getProperty()
    println(prop.getProperty("url"))
    println(prop.getProperty("author"))
    // get the property of "url" using get()
    println(prop.get("user"))
}
Output :
		https://chercher.tech
		unknown
		any one in the world

Reflection in kotlin

Properties files in text format

Sometimes properties could be in different format like '.txt' or other extensions, the file could have any extension still we can use the same methods what we used in .properties file with kotlin.

config file

config-p-text-properties-file-kotlin

This program shows how to read Properties files in text format

import java.io.FileInputStream
import java.util.*
fun main(args: Array<String>) {
    // create file input stream object for the properties file
    val fis = FileInputStream("C:pathconfig-p.txt")
    // create Properties class object to access properties file
    val prop = Properties()
    // load the properties file
    prop.load(fis)
    // get the property of "url" using getProperty()
    println(prop.getProperty("time"))
    // get the property of "url" using get()
    println(prop["hour"])
}

Output :

output-of-text-properties-file-kotlin

File Operations in kotlin

Write Properties File

Follow the below steps to write property file
1. Create a Kotlin file
2.