Ben*_*Ben 6 java append apache-commons java-io
公共文件FileUtils看起来很酷,我无法相信它们不能被附加到文件中.
File file = new File(path);
FileUtils.writeLines(file, printStats(new DateTime(), headerRequired));
Run Code Online (Sandbox Code Playgroud)
以上只是每次都替换文件的内容,我只想继续标记这些东西,就像这段代码一样.
fw = new FileWriter(file, true);
try{
for(String line : printStats(new DateTime(), headerRequired)){
fw.write(line + "\n");
}
}
finally{
fw.close();
}
Run Code Online (Sandbox Code Playgroud)
我搜索过javadoc但什么也没找到!我错过了什么?
它们现在是FileUtils类中的appendString(...)方法.
但是您可以从FileUtils.openOutputStream (...)获取Outputstream ,然后使用它来写入
write(byte[] b, int off, int len)
Run Code Online (Sandbox Code Playgroud)
您可以计算关闭,以便您将支持该文件.
编辑
在阅读Davids回答后,我认识到BufferedWriter将为您完成这项工作
BufferedWriter output = new BufferedWriter(new FileWriter(simFile));
output.append(text);
Run Code Online (Sandbox Code Playgroud)