Assignment not allow in while expression?

I personally prefer it this way

randomAccessFile.seek(offset)

while (true) {
    val byteCount = randomAccessFile.read(buffer)
    if (byteCount < 0) break
    fos.write(buffer, 0, byteCount)
}

IMHO, this is the most clear way to write this kind of code. No invisible side-effects, no code duplication, looping is explicit. No var at all, just a val.

As for lines, Kotlin’s null-safety features enable a much better idiom for all these kind of readLine iterations:

val reader = BufferedReader(reader)
while (true) {
    val line = reader.readLine() ?: break
    System.out.println(line);
}

I don’t miss Java-style pattern of using readLine at all. This is clearly much better. No vars and no nulls here.

17 Likes