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 provide few methods to read and write the data into properties file.

Insights about properties files

  • You can store the file anywhere in local file system, better if you can place it relative to you 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 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 properties file path, it makes the file into stream of input item

val fis = FileInputStream("C:\\path\\config.properties")


4. Create 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 patrameter

println(prop.getProperty("url"))


Complete Program to Read value from 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:\\path\\config.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 extension, 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:\\path\\config-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

File Operations in kotlin

Write Properties File

Follow below steps to write property file
1. Create a Kotlin file
2. Create a properties file on your local machine with .properties extension(config-write.properties). write-properties-file-kotlin

3. Create Object for FileOutputStream with .properties file path, it makes the file into stream of output item

val fis = FileOutputStream("C:\\path\\config-write.properties")		


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

val prop = Properties()


5. Set the values using getProperty("key","Value") method by passing key and values as the parameters

prop.setProperty("Selenium", "https://chercher.tech")


6. Store the values onto file commit it to local file system using prop.store(fos)


7. It will store the values along with the committed time on the first line


Complete Program to Write values to properties file with Kotlin


import java.util.*
import java.io.FileOutputStream
fun main(args: Array<String>) {
    // create file output stream object for the properties file
    val fis = FileOutputStream("C:\\path\\config-write.properties")
    // create Properties class object to access properties file
    val prop = Properties()
    // set the properties
    prop.setProperty("Selenium", "https://chercher.tech")
    prop.setProperty("Google", "https://google.com")
    prop.setProperty("Yahoo", "https://yahoo.com")
    // store the file into local system
    prop.store(fis, null)
}


output-properties-file-kotlin

Excel Files with apache poi

Read All Values in properties file

When we are reading a property file we can look into the .properties n-number of times but sometimes we may need to read the all the properties to verify something either on application or on the properties file.

config file read-all-properties-kotlin

This program shows how to Read All Values in properties file


import java.util.*
import java.io.FileInputStream
fun main(args: Array<String>) {
    // create file input stream object, to make file as readable by machine
    val fis = FileInputStream("C:\\path\\config-read-all.properties")
    // create properties class object to access all non-static methods
    val properties = Properties()
    // load the required .properties file
    properties.load(fis)
    // close the file as Properties class object have all the details
    fis.close()
    // Properties work like HashTable, so we have to handle like hash table only
    val enumKeys = properties.keys()
    // iterate till the enumKeys has keys
    while (enumKeys.hasMoreElements()) {
        // move from null to first element(key),
		// by default it will not point to first element
        val key = enumKeys.nextElement() as String
        // fetch the property for the key
        val value = properties.getProperty(key)
        println("$key = $value")
    }
}


output-read-all-properties-kotlin

CSV files

Read Multiple Properties Files in Kotlin

This methods not only for java but also you can use where ever you use Kotlin to read properties files.

There could be a scenarios where you have read values from properties file but you might have stored them in different files.

For example : login.properties, config.txt and so on, please Read Properties file in other formats if you need more information why we are using '.txt' file

Files read-multiple-properties-files-in-kotlin

1. Everything is similar to reading a single property file
2. Only difference is when you load your property file, we have load all the property files.


prop.load(fis);
prop.load(fis2);


Complete program to read Multiple properties files


import java.util.*
import java.io.FileInputStream
fun main(args: Array<String>) {
    // create file input stream object for the properties file
    val fis = FileInputStream("C:\\path\\login.properties")
    val fis2 = FileInputStream("C:\\path\\config.txt")
    // create Properties class object to access properties file
    val prop = Properties()
    // load the properties file
    prop.load(fis)
    //load all the files
    prop.load(fis2)
    // get the property of "url" using getProperty()
    println(prop.getProperty("url"))
    println(prop["suite"])
}
                        	

output-read-multiple-properties-files-in-kotlin

KFunction in Kotlin Reflection

Convert properties file values to Kotlin Map

When we are reading a property file, we can look into the .properties n-number of time but it takes lot of memory and process to read the value each time from the file.

To avoid this in our frameworks we have to keep all the properties in a map, so that we can retrieve the values from this map when it is required

This program shows how to Convert properties file values to Kotlin Map


import java.util.*
import java.io.FileInputStream
fun main(args: Array<String>) {
    // declare a map which store string as key and string as value
    val mapProp = hashMapOf()
    // create file input stream object,
	// to make file as readable by machine
    val fis = FileInputStream("C:\\path\\read-all.properties")
    // create properties class object to access all non-static methods
    val prop = Properties()
    // load the required .properties file
    prop.load(fis)
    // close the file as Properties class object have all the details
    fis.close()
    // Properties work like HashTable, 
	// so we have to handle like hash table only
    val enumKeys = prop.keys()
    // iterate till the enumKeys has keys
    while (enumKeys.hasMoreElements()) {
        // move from null to first element(key), 
		// by default it will not point to first element
        val key = enumKeys.nextElement() as String
        // fetch the property for the key
        val value = prop.getProperty(key)
        // store the key and value in map
        mapProp[key] = value
    }
    println("Value stored to Map are : $mapProp")
}

Output :
output-properties-file-kotlin-map

Convert Kotlin Map to properties file

Sometimes we may in a situation where we have to store the kotlin map to properties file, end of the we have to store the value for retrieving in future.

config file read-all-properties-kotlin

This program shows how to Convert Kotlin Map to properties file


import java.util.*
import java.io.FileOutputStream
import java.io.File
fun main(args: Array<String>) {
    // declare a map which store string as key and string as value
    val mapProp = hashMapOf()
    mapProp["Name"] = "unknow"
    mapProp["Age"] = "27"
    mapProp["Gende"] = "Male"
    val file = File("C:\\path\\map-to-file.properties")
    // create file input stream object for the properties file
    val fos = FileOutputStream(file)
    // create Properties class object to access properties file
    val prop = Properties()
    val keys = mapProp.keys.iterator()
    while (keys.hasNext()) {
        val key = keys.next()
        prop.setProperty(key, mapProp[key])
    }
    prop.store(fos, null)
}
						

Output :
kotlin-map-to-properties-file

Map Collections in Kotlin

aaaaaaaaaaaaa
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Selenium Online Training : Our next online training course for Selenium with Java starts from 17th December 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn, then you can join the course by sending email to chercher.tech@gmail.com

or Register below


 
Join My Facebook Group
Join Group