Here is code in Scala:
def write() = {
try {
val out = new PrintWriter(new BufferedWriter(new FileWriter(fileName, true)))
out.println(\"123\")
Or just:
val is = new FileInputStream(file)
val result = try {
// do stuff
} finally {
is.close()
}
Because there's no way is
can be null.
In your question code, just move val out
outside try block. That way it will be identical to what Java AutoCloseable does, except for null case. But in Scala you don't have to deal with nullables.