In my Kotlin project in folder resources I has properties file. How I can read content data from this file to Properties object?
I try this:
val fis = FileInputStream("resources/pairs_ids.txt")
prop.load(fis);
logger.info("ETH_BTC_id = " + prop.get("ETH_BTC"))
But I get error:
Exception in thread "main" java.io.FileNotFoundException: resources\pairs_ids.txt (The system cannot find the path specified)
what I like to do in this case is something like:
@Suppress("UNCHECKED_CAST")
fun <T> getProp(key: String): T {
val props = javaClass.classLoader.getResourceAsStream("pairs_ids.txt").use {
Properties().apply { load(it) }
}
return (props.getProperty(key) as T) ?: throw RuntimeException("could not find property $key")
}
it will read the properties and tries to cast a certain property. because of kotlins type inference it can then be used like this:
val foo: String = getProp("ETH_BTC")
or this:
val foo = getProp<String>("ETH_BTC")
val props = Properties()
props.load(...)
props.getProperty("key")
That is the same as with Java