Kotlin read properties file from/to .Properties/.XML files

Kotlin read properties file

In the tutorial: “Kotlin read properties file”, We show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.

Kotlin Write Properties to .properties file by 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.

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) {
	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

Kotlin Read Properties from .Properties file using 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. 

Full 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) {
		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
		 */
}

Write Properties to .XML file using 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. 

Full 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) {
		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>

Read Properties from .XML file using 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. 

Full 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) {
	
	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
	 */
}

Further Reading

Related posts:


Leave a Reply

Your email address will not be published. Required fields are marked *