I am doing a bit of competitive programming in koltin. Most of the time I used input from the console but sometimes I want to use files. Is there a way to make readln() work from a file ? The goal is to avoid writting to code doing the same thing.
From here: Reading console input in Kotlin I tries
fun <T : Closeable, R> T.useWith(block: T.() -> R): R = use { with(it, block) }
File("a.in").bufferedReader().useWith {
File("a.out").printWriter().useWith {
val (a, b) = readLine()!!.split(' ').map(String::toInt)
println(a + b)
}
}
Scanner(File("b.in")).useWith {
PrintWriter("b.out").useWith {
val a = nextInt()
val b = nextInt()
println(a + b)
}
}
But I was not able to make it works.
Thx for any answer.