读取文件
Source.fromFile(“相对路径/绝对路径”).foreach(输出类型)
object test_stdin {
def main(args: Array[String]): Unit = {
//1.从文件中读取数据
//绝对路径
Source.fromFile("C:\\workspace\\scala_test\\src\\main\\resources\\1.txt").foreach(print)
//相对路径
Source.fromFile("src/main/resources/1.txt").foreach(print)
}
}
写入文件
object test_stdin {
def main(args: Array[String]): Unit = {
//将数据写入文件
val writer = new PrintWriter(new File("src/main/resources/out.txt"))
writer.write("hello world")
writer.close()
}
}