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

标签 java text io

Possible Duplicate:
How to append text to an existing file in Java

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

最佳答案

来自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 使用默认编码,该编码根据安装的不同而变化。

关于java - 如何将字符串添加到 .txt 文件末尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3253728/

相关文章:

java - 找到 Selenium 按钮但未执行单击

javascript - 获取文本输入中选定文本的索引

c# - 如何处理文件名中的空格

C I/O 程序不工作

text - iphone/ipod touch对canvas标签有什么样的支持?尤其是在文字方面?

c++ - 如何在 Ubuntu 上提高我的程序磁盘读取速度?

java - "Creator"配置继承对象的模式

java - 如何定义用户定义的 java 运行时可选命令行参数

java - 返回具有特定属性 JDOM 的元素

c - 如何使用 strtok 从文本文件打印到控制台单个字符?