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

时间:2010-06-03 13:24:45

标签: java file-io apache-commons

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

7 个答案:

答案 0 :(得分:53)

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

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

例如:

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

答案 1 :(得分:5)

下载最新版本Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

设置追加到真......

答案 2 :(得分: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() {}

}

答案 3 :(得分: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() {
    }

}

编辑:我的日食坏了,所以它没有告诉我早些时候的错误。修正错误

答案 4 :(得分:2)

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

Here's the Javadoc

maven依赖:

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

答案 5 :(得分:1)

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

FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);

答案 6 :(得分: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