33

Apache Commons I/O的FileUtils.writeStringToFile(fileName, text)功能覆盖文件中以前的文本。我想将数据附加到我的文件中。有什么方法可以使用 Commons I/O 吗?我可以使用BufferedWriterJava 中的普通方法来做到这一点,但我对使用 Commons I/O 也很好奇。

4

7 回答 7

65

它已经在 2.1 版本的 Apache IO 中实现。要将字符串附加到文件中,只需将true作为函数中的附加参数传递:

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

前任:

    FileUtils.writeStringToFile(file, "String to append", true);
于 2011-11-28T10:44:11.893 回答
5

下载最新版本 Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

将追加设置为真....

于 2011-12-02T13:44:12.267 回答
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() {}

}
于 2011-03-12T00:26:32.943 回答
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() {
    }

}

编辑:我的日食坏了,所以它之前没有向我显示错误。修复错误

于 2010-06-04T08:50:15.433 回答
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>
于 2015-06-20T01:48:48.890 回答
1

在 2.5 版中,您需要传递一个额外的参数,即编码。

FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);
于 2017-07-23T02:40:47.280 回答
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
于 2011-11-28T11:03:05.887 回答