【问题标题】:Add a new line of text to an existing file in scala向 scala 中的现有文件添加新的文本行
【发布时间】:2016-01-07 05:57:53
【问题描述】:

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

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

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

请帮帮我。

【问题讨论】:

    标签: scala


    【解决方案1】:

    这是一个java api问题

    你可以的

    val write = new PrintWriter(new FileOutputStream(new File("Test.txt"),true)))
    

    这将以追加模式而不是覆盖模式打开文件。

    文档是here

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-04
    • 1970-01-01
    • 2021-06-08
    相关资源
    最近更新 更多