更新时间: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.