java 如果文件存在,是否可以将 apache FileUtils.writeLines() 附加到文件中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1629713/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:20:34  来源:igfitidea点击:

Can apache FileUtils.writeLines() be made to append to a file if it exists

javaappendapache-commonsjava-io

提问by Ben

The commons FileUtilslook pretty cool, and I can't believe that they cannot be made to append to a file.

Commons FileUtils看起来很酷,我不敢相信它们不能附加到文件中。

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?

我搜索了 javadoc 但什么也没找到!我错过了什么?

回答by David Rabinowitz

You can use IOUtils.writeLines(), it receives a Writer object which you can initialize like in your second example.

您可以使用IOUtils.writeLines(),它接收一个 Writer 对象,您可以像在第二个示例中一样对其进行初始化。

回答by Markus Lausberg

Their is now method like appendString(...) in the FileUtils class.

他们现在是 FileUtils 类中的 appendString(...) 方法。

But you can get the Outputstream from FileUtils.openOutputStream(...)and than write to it by using

但是您可以从 FileUtils.openOutputStream (...)获取输出流,然后使用

write(byte[] b, int off, int len) 

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

阅读 Davids 的回答后,我意识到 BufferedWriter 会为您完成这项工作

BufferedWriter output = new BufferedWriter(new FileWriter(simFile));
output.append(text);

回答by Fred Haslam

As of apache FileUtils 2.1 you have this method:

从 apache FileUtils 2.1 开始,你有这个方法:

static void write(File file, CharSequence data, boolean append)

静态无效写入(文件文件,CharSequence 数据,布尔追加)

回答by Kris

There is an overload for FileUtils.writelines() which takes parameter on whether to append.

FileUtils.writelines() 有一个重载,它接受关于是否追加的参数。

public static void writeLines(File file,
          Collection<?> lines,
          boolean append)
                   throws IOException

file - the file to write to

file - 要写入的文件

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

append - 如果为真,那么这些行将被添加到文件的末尾而不是覆盖

Case1:

情况1:

FileUtils.writeLine(file_to_write, "line 1");
FileUtils.writeLine(file_to_write, "line 2");

file_to_write will contain only

file_to_write 将只包含

line 2

As first writing to the same file multiple times will overwrite the file.

因为第一次写入同一个文件多次将覆盖该文件。

Case2:

案例2:

FileUtils.writeLine(file_to_write, "line 1");
FileUtils.writeLine(file_to_write, "line 2", true);

file_to_write will contain

file_to_write 将包含

line 1
line 2

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)

从 java 官方文档中查看更多详细信息。 https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html#writeLines(java.io.File, java.util.Collection, boolean)

回答by Dilip Raghunathan

you could do something like

你可以做类似的事情

List x = FileUtils.readLines(file);
x.add(String)
FileUtils.writelines(file,x);