如何将字符串添加到.txt文件的末尾?

时间:2010-07-15 08:24:03

标签: java text io

  

可能重复:
  How to append text to an existing file in Java

如何在.txt文件的末尾添加字符串?

2 个答案:

答案 0 :(得分:5)

来自here

BufferedWriter bw = null;

try {
    bw = new BufferedWriter(new FileWriter("checkbook.txt", true));
    bw.write("400:08311998:Inprise Corporation:249.95");
    bw.newLine();
    bw.flush();
} catch (IOException ioe) {
    ioe.printStackTrace();
} finally { // always close the file
    if (bw != null) {
        try {
            bw.close();
        } catch (IOException ioe2) {
            // just ignore it
        }
    }
}

根据Joachim Sauer的建议,您可以使用

而不是FileWriter
new OutputStreamWriter(new FileOutputStream(..), ecnoding)

如果要指定文件的编码。 FileWriter使用默认编码,该编码从安装更改为安装。

答案 1 :(得分:2)

要查找的英语术语:“追加”

您需要执行以下步骤:

  1. 打开文件。
  2. 追加字符串。
  3. 关闭文件。
  4. 了解FileOutputStream课程。

相关问题