To read a text file, create a scala.io.Source object like this:
val f = scala.io.Source.fromFile(fname)Or, if you think the class name is too long, import the class first:
import scala.io.Source val f = Source.fromFile(fname)The most important method of the Source object f is getLines, which allows you to iterate over all the lines of the file:
val fname = args(0) val f = scala.io.Source.fromFile(fname) for (line <- f.getLines()) printf("%4d %s\n", line.length, line) f.close()The lines returned in the line variable contain no new-line characters anymore.
You should call f.close() when you are done with the Source.