Files and Text Streams
Resource Cleanup
Model opening, using, and closing a resource with explicit state.
Resource Cleanup
ResourceCleanup.kt
fun main() {
val chunk =
var state = "open"
val opened = state == "open"
val readText = "read:$chunk"
state = "closed"
val ready = state == "closed"
println("opened=$opened")
println("read=$readText")
println("state=$state")
println("ready=$ready")
}
fun main() {
val chunk =
var state = "open"
val opened = state == "open"
val readText = "read:$chunk"
state = "closed"
val ready = state == "closed"
println("opened=$opened")
println("read=$readText")
println("state=$state")
println("ready=$ready")
}
fun main() {
val chunk =
var state = "open"
val opened = state == "open"
val readText = "read:$chunk"
state = "closed"
val ready = state == "closed"
println("opened=$opened")
println("read=$readText")
println("state=$state")
println("ready=$ready")
}
resource-cleanup
Cleanup code should leave a resource in a known final state.