Scala读文件使用scala.io模块,写文件使用java.io模块
写文件
import java.io.PrintWriter
@Test
def testFileI(): Unit ={
/**
* 测试写文件
*/
val printWriter:PrintWriter=new PrintWriter("./src/main/resources/test.txt");
printWriter.println("床前明月光")
printWriter.println("疑是地上霜")
printWriter.println("举头望明月")
printWriter.println("低头思故乡")
printWriter.close()
}
读文件
import scala.io.{BufferedSource, Source}
@Test
def testFileO(): Unit ={
/**
* 测试读文件
*/
val sourceFile:BufferedSource=Source.fromFile("./src/main/resources/test.txt")
for(line<-sourceFile.getLines()){
println(line)
}
sourceFile.close()
}