apache FileUtils.writeLines() 是否可以追加到文件(如果存在)
公共资源 FileUtils看起来很酷,我不敢相信它们不能附加到文件中。
File file = new File(path);
FileUtils.writeLines(file, printStats(new DateTime(), headerRequired));
上面只是每次替换文件的内容,我只想继续标记这些东西以结束,就像这段代码一样。
fw = new FileWriter(file, true);
try{
for(String line : printStats(new DateTime(), headerRequired)){
fw.write(line + "\n");
}
}
finally{
fw.close();
}
我搜索了 javadoc 但什么也没找到!我缺少什么?
The commons FileUtils look pretty cool, and I can't believe that they cannot be made to append to a file.
File file = new File(path);
FileUtils.writeLines(file, printStats(new DateTime(), headerRequired));
The above just replaces the contents of the file each time, and I would just like to keep tagging this stuff to end just as this code does.
fw = new FileWriter(file, true);
try{
for(String line : printStats(new DateTime(), headerRequired)){
fw.write(line + "\n");
}
}
finally{
fw.close();
}
I've searched the javadoc but found nothing! What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以使用 IOUtils.writeLines(),它接收一个 Writer 对象,您可以像第二个示例中那样初始化该对象。
You can use IOUtils.writeLines(), it receives a Writer object which you can initialize like in your second example.
他们现在的方法类似于 FileUtils 类中的appendString(...)。
但是您可以从 FileUtils.openOutputStream(...) 获取输出流,然后使用
您可以计算关闭来写入它,以便您将附加到文件。
编辑
阅读 Davids 的回答后,我认识到 BufferedWriter 将为您完成这项工作
Their is now method like appendString(...) in the FileUtils class.
But you can get the Outputstream from FileUtils.openOutputStream(...) and than write to it by using
You can calculte the off, so that you will apend to the file.
EDIT
After reading Davids answer i recognized that the BufferedWriter will do this job for you
从 apache FileUtils 2.1 开始,您有这个方法:
static void write(File file, CharSequence data, boolean append)
As of apache FileUtils 2.1 you have this method:
static void write(File file, CharSequence data, boolean append)
FileUtils.writelines() 有一个重载,它接受是否追加的参数。
文件 - 要写入的文件 行
- 要写入的行,空条目会产生空行
追加 - 如果为 true,则这些行将被添加到文件末尾而不是覆盖
情况 1:
file_to_write 将仅包含
作为第一次写入同一文件多次会覆盖该文件。
Case2:
file_to_write 将包含
第二次调用将追加到同一个文件。
从 java 官方文档中查看更多详细信息。
https ://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeLines(java.io.File,java.util.Collection,布尔值)
There is an overload for FileUtils.writelines() which takes parameter on whether to append.
file - the file to write to
lines - the lines to write, null entries produce blank lines
append - if true, then the lines will be added to the end of the file rather than overwriting
Case1:
file_to_write will contain only
As first writing to the same file multiple times will overwrite the file.
Case2:
file_to_write will contain
The second call will append to the same file.
Check this from java official documentation for more details.
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeLines(java.io.File, java.util.Collection, boolean)
你可以做类似的事情
you could do something like