在scala中的现有文件中添加一行新文本

Sai*_*Sai 0 scala

我是Scala的新手.我想在现有文件中添加一行新文本.

我已经尝试了下面的代码,但它覆盖了现有的文本:

println("plese enter the text")
val text = Console.readLine()
val write = new PrintWriter(new File("Test.txt"))
write.write(text) 
write.close()
Run Code Online (Sandbox Code Playgroud)

请帮我解决一下这个.

小智 6

这是一个java api问题

你可以做

val write = new PrintWriter(new FileOutputStream(new File("Test.txt"),true)))
Run Code Online (Sandbox Code Playgroud)

这将以附加模式打开文件而不是覆盖模式.

文档在这里