Append data into a file using Apache Commons I/O

2019-01-18 02:01发布

问题:

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 BufferedWriter from Java but I'm curious regarding the same using Commons I/O.

回答1:

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

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

ex:

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


回答2:

Download the latest version Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

set append to true....



回答3:

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

}


回答4:

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



回答5:

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

Here's the Javadoc

And the maven dependency:

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


回答6:

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

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


回答7:

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