scala读取文本文件的内容的例子:
package demo.fileoper import scala.io.Source object FileReaderDemo { def main(args: Array[String]): Unit = { val filePath = "D:\\idea_workspace\\scala-examples\\Demos\\test.txt" println("以下打印的是文件中的内容:") Source.fromFile(filePath).foreach{ print } } }
运行结果:
以下打印的是文件中的内容:
Hello Scala
This is a text file.
The contents in it is just for some test.
你好,很高兴能见到你。