In the post, we show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.
Contents
I. Kotlin – Read/Write Properties from/to .Properties file
1. Write Properties to .Properties file
1.1 Properties store() method
We use java.util.Properties.store()
methods:
// 1. fun store(out: OutputStream, comments: String): Unit -> Writes this property list (key and element pairs) in this Properties table to the output stream in a format suitable for loading into a Properties table using the load(InputStream) method. // 2. fun store(writer: Writer, comments: String): Unit -> Writes this property list (key and element pairs) in this Properties table to the output character stream in a format suitable for using the load(Reader) method. |
1.2 Kotlin Program – write Properties to .properties file
package com.javasampleapproach.kotlin.properties import java.io.FileOutputStream import java.io.FileWriter import java.io.IOException import java.util.Properties fun main(args: Array<String>) { val properties = Properties() properties.put("db.username", "username") properties.put("db.password", "password") properties.put("db.driver", "org.postgresql.Driver") properties.put("db.url", "jdbc:postgresql://localhost/testdb") var propertiesFile = System.getProperty("user.dir") + "\\file.properties" /* * Approach 1: * use -> 'java.util.Properties.store(out: OutputStream, comments: String)' */ var fileOutputStream = FileOutputStream(propertiesFile) properties.store(fileOutputStream, "save to properties file") /* * Approach 2: * use -> 'java.util.Properties.store(writer: Writer, comments: String)' */ propertiesFile = System.getProperty("user.dir") + "\\file_1.properties" val fileWriter = FileWriter(propertiesFile) properties.store(fileWriter, "save to properties file") } |
-> .properties
output file:
#save to properties file #Tue Mar 06 21:31:28 ICT 2018 db.password=password db.url=jdbc\:postgresql\://localhost/testdb db.username=username db.driver=org.postgresql.Driver |
2. Read Properties from .Properties file
2.1 Properties load() method
We use java.util.Properties.load()
methods:
// 1. fun load(inStream: InputStream): Unit -> Reads a property list (key and element pairs) from the input byte stream. The input stream is in a simple line-oriented format as specified in load(Reader) and is assumed to use the ISO 8859-1 character encoding; that is each byte is one Latin1 character. // 2. fun load(reader: Reader): Unit -> Reads a property list (key and element pairs) from the input character stream in a simple line-oriented format. |
2.2 Kotlin Program – Read .Properties File to Properties object
package com.javasampleapproach.kotlin.properties import java.io.FileInputStream import java.io.FileReader import java.io.IOException import java.util.Properties fun main(args: Array<String>) { val properties = Properties() val propertiesFile = System.getProperty("user.dir") + "\\file.properties"; /* * Approach 1: * -> use 'java.util.Properties.load(inStream: InputStream)' */ val inputStream = FileInputStream(propertiesFile) properties.load(inputStream) properties.forEach{(k, v) -> println("key = $k, value = $v")} /* key = db.driver, value = org.postgresql.Driver key = db.username, value = username key = db.url, value = jdbc:postgresql://localhost/testdb key = db.password, value = password */ println("----------------------------"); /* * Approach 2: * -> use 'java.util.Properties.load(reader: Reader)' */ val reader = FileReader(propertiesFile) properties.load(reader) properties.forEach{(k, v) -> println("key = $k, value = $v")} /* key = db.driver, value = org.postgresql.Driver key = db.username, value = username key = db.url, value = jdbc:postgresql://localhost/testdb key = db.password, value = password */ } |
II. Kotlin – Read/Write Properties from/to .XML file
1. Write Properties to .XML file
1.1 Properties.storeToXML() method
We use java.util.Properties.storeToXML()
methods:
// 1. fun storeToXML(os: OutputStream, comment: String): Unit -> Emits an XML document representing all of the properties contained in this table. An invocation of this method of the form props.storeToXML(os, comment) behaves in exactly the same way as the invocation props.storeToXML(os, comment, "UTF-8");. // 2. fun storeToXML(os: OutputStream, comment: String, encoding: String): Unit -> Emits an XML document representing all of the properties contained in this table, using the specified encoding. |
1.2 Kotlin Program – Write Properties object to XML file
package com.javasampleapproach.kotlin.properties import java.io.FileOutputStream import java.io.IOException import java.nio.charset.StandardCharsets import java.util.Properties fun main(args: Array<String>) { val properties = Properties() properties.put("db.username", "username") properties.put("db.password", "password") properties.put("db.driver", "org.postgresql.Driver") properties.put("db.url", "jdbc:postgresql://localhost/testdb") /* * Approach 1: * use -> 'java.util.Properties.storeToXML(os: OutputStream, comment: String)' */ var propertiesFile = System.getProperty("user.dir") + "\\file.xml" var fileOutputStream = FileOutputStream(propertiesFile) properties.storeToXML(fileOutputStream, "save to properties file") /* * Approach 2: * use -> 'java.util.Properties.storeToXML(os: OutputStream, comment: String, encoding: String)' */ propertiesFile = System.getProperty("user.dir") + "\\file_1.xml"; fileOutputStream = FileOutputStream(propertiesFile) properties.storeToXML(fileOutputStream, "save to XML file with UTF-8 enconding", StandardCharsets.UTF_8.toString()); } |
-> .xml
output file:
<?xml version="1.0" encoding="UTF-8" standalone="no"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>save to XML file with UTF-8 enconding</comment> <entry key="db.password">password</entry> <entry key="db.url">jdbc:postgresql://localhost/testdb</entry> <entry key="db.username">username</entry> <entry key="db.driver">org.postgresql.Driver</entry> </properties> |
2. Read Properties from .XML file
2.1 Properties.loadFromXML() method
We use java.util.Properties.loadFromXML()
method:
fun loadFromXML(in: InputStream): Unit -> Loads all of the properties represented by the XML document on the specified input stream into this properties table. |
2.2 Kotlin Program – Read XML file to Properties object
package com.javasampleapproach.kotlin.properties import java.io.FileInputStream import java.io.IOException import java.util.Properties fun main(args: Array<String>) { val properties = Properties() var propertiesFile = System.getProperty("user.dir") + "\\file.xml" /* * use 'java.util.Properties.loadFromXML(in: InputStream) */ val inputStream = FileInputStream(propertiesFile) properties.loadFromXML(inputStream) properties.forEach{(k, v) -> println("key = $k, value = $v")} /* key = db.driver, value = org.postgresql.Driver key = db.username, value = username key = db.url, value = jdbc:postgresql://localhost/testdb key = db.password, value = password */ } |