读取文件

Source.fromFile(“相对路径/绝对路径”).foreach(输出类型)

  1. object test_stdin {
  2. def main(args: Array[String]): Unit = {
  3. //1.从文件中读取数据
  4. //绝对路径
  5. Source.fromFile("C:\\workspace\\scala_test\\src\\main\\resources\\1.txt").foreach(print)
  6. //相对路径
  7. Source.fromFile("src/main/resources/1.txt").foreach(print)
  8. }
  9. }

写入文件

  1. object test_stdin {
  2. def main(args: Array[String]): Unit = {
  3. //将数据写入文件
  4. val writer = new PrintWriter(new File("src/main/resources/out.txt"))
  5. writer.write("hello world")
  6. writer.close()
  7. }
  8. }