在Scala中,可以使用以下方法來讀取文件:
import scala.io.Source
val filename = "file.txt"
val fileContents = Source.fromFile(filename).getLines().mkString("\n")
println(fileContents)
import java.nio.file.{Paths, Files}
val filename = "file.txt"
val fileContents = new String(Files.readAllBytes(Paths.get(filename)))
println(fileContents)
import java.io._
val filename = "file.txt"
val reader = new BufferedReader(new FileReader(filename))
var line: String = null
while ({line = reader.readLine(); line != null}) {
println(line)
}
reader.close()