With Joshua Bloch’s recently updated book, Effective Java, he talks about avoiding the use of finalizers and, if running Java 9, their replacement cleaners. One suggestion is to use
try-with-resources, which doesn’t exist in Kotlin.
Instead, Kotlin introduces the extension function
use which will call
close() on a
Closeable when the block provided completes or throws an exception.
// do something with the stream
If using a class that doesn’t extend
Closeable, take a look at the implementation of use as it is easy enough to make an extension method to replicate this functionality.