I'm looking to find the last line of a text file using a rather standard while-loop idiom I find often used in Java.
I have a less compact version working. But the one I would like to use does not appear to be valid syntax in Kotlin. My preferred method includes an assignment and a Boolean test on that assignment in the same line.
Admittedly this is a small matter, but I'm looking to better implement my Kotlin code.
fun readLastLine(file:File):String {
val bufferedReader = file.bufferedReader()
var lastLine=""
//valid
var current = bufferedReader.readLine()
while(current != null) {
lastLine=current
current = bufferedReader.readLine()
}
//return lastLine
//not valid...
//while((current=bufferedReader.readLine())!=null){
// lastLine=current
//}
//responding to comment below,
//preferred/terse answer using file.readLines
//this reads all the lines into a list, then returns the last
return file.readLines().last()
}
file.readLines().last()