[Kotlin] Try resource, use {} block

In Java, when you write code in try(...), if exception throw, the resource will be closed automactially:

This is not necessary in Kotlin

fun printFile() {
    val input = FileInputStream("file.txt")
    input.use {
        // an exception can be thrown here
        // and input resource will be closed automaticlly
    }
}
原文地址:https://www.cnblogs.com/Answer1215/p/13900102.html