使用Apache Commons I/O将数据追加到文件中

33
< p > Apache Commons I/O 的< code >FileUtils.writeStringToFile(fileName, text)函数会覆盖文件中先前的文本内容。我想要在文件中追加数据,是否有办法可以使用 Commons I/O 完成同样的功能?我可以使用 Java 中的普通 BufferedWriter 完成此操作,但是我对使用 Commons I/O 实现此操作感到好奇。


1
我认为这方面没有预先制作的方法。当然,你可以使用readLines(...).append(myLines)。但我猜你说的是一个可能非常大的文件。 - extraneon
1
是的!我有27k个文件,其中大约有900,00篇文章。在这里寻找更多创新! - Dexter
1
这个问题已经有一个开放的功能请求(https://issues.apache.org/jira/browse/IO-182)。不幸的是,它自2008年9月以来一直处于开放状态,并没有任何人真正着手解决它的迹象。 - skaffman
5
自从这篇文章写出来后,commons-io FileUtils 2.1 已经更新以支持向文件追加内容。writeStringToFile(File file, String data, boolean append) - Mark McLaren
1
@DaSh 我已将其标记为已接受。 - Dexter
显示剩余3条评论
7个回答

67

这已经在Apache IO 2.1版本中实现了。

要将字符串附加到文件中,只需在以下函数中作为额外参数传递true

  • FileUtils.writeStringToFile
  • FileUtils.openOutputStream
  • FileUtils.write
  • FileUtils.writeByteArrayToFile
  • FileUtils.writeLines

例如:

    FileUtils.writeStringToFile(file, "String to append", true);

谢谢你的回答。+1 - Diganta
2
writeStringToFile() 应该在第三个位置有另一个参数:要使用的 Charset。当前版本的 Apache IO 报告上述方法已被弃用。 - vektor

5

下载最新版本的Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

将追加设置为true....


4
小心。该实现似乎存在文件句柄泄漏...
public final class AppendUtils {

    public static void appendToFile(final InputStream in, final File f) throws IOException {
        OutputStream stream = null;
        try {
            stream = outStream(f);
            IOUtils.copy(in, stream);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    public static void appendToFile(final String in, final File f) throws IOException {
        InputStream stream = null;
        try {
            stream = IOUtils.toInputStream(in);
            appendToFile(stream, f);
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    private static OutputStream outStream(final File f) throws IOException {
        return new BufferedOutputStream(new FileOutputStream(f, true));
    }

    private AppendUtils() {}

}

+1。现在,我们只需要一个具有新资源管理风格的Java 7样式实用程序:http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html ... 还有,这些东西为什么没有包含在Apache Commons或其他库中? - ripper234
啊,没关系,现在FileUtils实际上包含了FileUtils.writeStringToFile(); 请投票支持https://dev59.com/gHA85IYBdhLWcg3wCfHm#8294642 - ripper234

2

实际上,apache-commons-io FileUtils的2.4版本现在也具有了对集合的追加模式。

这是Javadoc

以下是Maven依赖:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.4</version>
    <type>jar</type>
</dependency>

2
在2.5版本中,您需要传递一个额外的参数,即编码。
FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);

2
在JDK8中使用StandardCharsets.UTF_8而不是字符串常量"UTF-8"。 - Míra

2
这个小东西应该能解决问题:
package com.yourpackage;

// you're gonna want to optimize these imports
import java.io.*;
import org.apache.commons.io.*;

public final class AppendUtils {

    public static void appendToFile(final InputStream in, final File f)
            throws IOException {
        IOUtils.copy(in, outStream(f));
    }

    public static void appendToFile(final String in, final File f)
            throws IOException {
        appendToFile(IOUtils.toInputStream(in), f);
    }

    private static OutputStream outStream(final File f) throws IOException {
        return new BufferedOutputStream(new FileOutputStream(f, true));
    }

    private AppendUtils() {
    }

}

编辑:我的eclipse出了问题,所以之前没有显示错误。已经修复错误。


0
public static void writeStringToFile(File file,
                                     String data,
                                     boolean append)
                              throws IOException


   Writes the toString() value of each item in a collection to the specified File line by line. The default VM encoding and the default line ending will be used.

Parameters:
    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 
Throws:
    IOException - in case of an I/O error
Since:
    Commons IO 2.1

在这种情况下如何关闭输出流? - Rishi

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接