且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何附加到文本文件而不是用Java覆盖它?

更新时间:2023-09-14 12:12:58

更改此:

fw = new FileWriter("output.txt");

fw = new FileWriter("output.txt", true);

参见 javadoc 了解详细原因 - 实际上append默认为false。

See the javadoc for details why - effectively the "append" defaults to false.

请注意 FileWriter 通常不是一个很好的类 - 我更喜欢使用 FileOutputStream 包装在 OutputStreamWriter ,因为它允许您指定要使用的字符编码,而不是使用您的操作系统默认值。

Note that FileWriter isn't generally a great class to use - I prefer to use FileOutputStream wrapped in OutputStreamWriter, as that lets you specify the character encoding to use, rather than using your operating system default.