在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()
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: scala怎么实现加减乘除功能