Scala code to read a text file to an Array (or Seq)
By Alvin Alexander. Last updated: October 20 2019
As a quick note, I use code like this read a text file into an Array
, List
, or Seq
using Scala:
def readFile(filename: String): Seq[String] = { val bufferedSource = io.Source.fromFile(filename) val lines = (for (line <- bufferedSource.getLines()) yield line).toList bufferedSource.close lines }