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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2966333/
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-08-13 14:59:28  来源:igfitidea点击:

Append data into a file using Apache Commons I/O

javafile-ioapache-commons

提问by Dexter

The FileUtils.writeStringToFile(fileName, text)function of Apache Commons I/O overwrites previous text in a file. I would like to append data to my file. Is there any way I could use Commons I/O for the same? I can do it using normal BufferedWriterfrom Java but I'm curious regarding the same using Commons I/O.

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

采纳答案by JJ Roman

It has been implemented in 2.1 version of Apache IO. To append string to the file just pass trueas an additional parameter in functions:

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

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

ex:

前任:

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

回答by Sean Patrick Floyd

this little thingy should do the trick:

这个小东西应该可以解决问题:

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() {
    }

}


edit: my eclipse was broken, so it didn't show me the errors earlier. fixed errors

编辑:我的日食坏了,所以它没有更早地向我显示错误。修正错误

回答by Ryan

Careful. That implementation seems to be leaking a file handle...

小心。该实现似乎正在泄漏文件句柄......

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() {}

}

回答by White_Sox

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

回答by Makky

Download the latest version Commons-io 2.1

下载最新版 Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

set append to true....

将附加设置为真....

回答by gps

Actually, version 2.4 of apache-commons-io FileUtils now has append mode for collections as well.

实际上,apache-commons-io FileUtils 的 2.4 版现在也具有用于集合的附加模式。

Here's the Javadoc

这是 Javadoc

And the maven dependency:

和 maven 依赖:

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

回答by Roushan

in version 2.5 you need to pass one extra parameter i.e, encoding.

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

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