Scala's io
library was just hack done to provide support for limited needs. There was an effort to provide a well-thought io library to Scala, which is currently hosted at assembla, with a github repository as well.
If you are going to use I/O for anything more than reading the occasional file on short-lived processes, you'd better either use Java libraries, or look at the I/O support presently available in the compiler (which will require scala-compiler.jar to be distributed with the app).
Automatic resource management is provided since Scala 2.13 in the standard library (scala.util.Using
). For older Scala versions look at this question, or at this library (which is featured in the accepted answer at that question).
So, on Scala 2.13 or newer, this works correctly:
import scala.util.Using
import scala.io.Source
val tryStr: Try[String] = Using(Source.fromFile("test.txt"))(_.mkString())