最佳答案
来自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/