2 years ago

#44699

test-img

Joon Hee Song

Kotlin Stream closed when using use clause in while loop

My code is simple.
When I execute Main function, I can enter number for only once.
If I enter second number, IOException: Stream closed occurs.
Can you explain why?

import java.io.*

fun main() {
    for (i in 0 until 5)
        val number = readLine().toInt()
}

fun readLine(): String {
    BufferedReader(InputStreamReader(System.`in`)).use { reader ->
        return reader.readLine()
    }
}

kotlin

try-with-resources

0 Answers

Your Answer

Accepted video resources