apache FileUtils.writeLines() 是否可以追加到文件(如果存在)

发布于 2024-08-08 14:21:10 字数 570 浏览 3 评论 0原文

公共资源 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

裂开嘴轻声笑有多痛 2024-08-15 14:21:10

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

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

蓝眼泪 2024-08-15 14:21:10

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

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

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

您可以计算关闭来写入它,以便您将附加到文件。

编辑

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

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

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

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

BufferedWriter output = new BufferedWriter(new FileWriter(simFile));
output.append(text);
梦里的微风 2024-08-15 14:21:10

从 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)

待天淡蓝洁白时 2024-08-15 14:21:10

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

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

文件 - 要写入的文件 行

- 要写入的行,空条目会产生空行

追加 - 如果为 true,则这些行将被添加到文件末尾而不是覆盖

情况 1:

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

file_to_write 将仅包含

line 2

作为第一次写入同一文件多次会覆盖该文件。

Case2:

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

file_to_write 将包含

line 1
line 2

第二次调用将追加到同一个文件。

从 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.

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

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:

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

file_to_write will contain only

line 2

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

Case2:

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

file_to_write will contain

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)

幸福%小乖 2024-08-15 14:21:10

你可以做类似的事情

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

you could do something like

List x = FileUtils.readLines(file);
x.add(String)
FileUtils.writelines(file,x);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文