33

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.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Dexter
  • 11,311
  • 11
  • 45
  • 61
  • 1
    I don't think there's a pre-made method for that. You can of course do readLines(...).append(myLines) but I suppose you're talking about a potentially very large file. – extraneon Jun 03 '10 at 13:31
  • 1
    Yes! I have 27k files with about 900,00 posts in it. Looking for more innovation here ! – Dexter Jun 04 '10 at 07:16
  • 1
    There's an open feature request in for this (https://issues.apache.org/jira/browse/IO-182). Unfortunately, it's been open since September 2008, and shows no sign of anyone actually pulling their finger out and doing it. – skaffman Jun 04 '10 at 09:24
  • Strange! considering the development is active on the project. – Dexter Jun 07 '10 at 05:49
  • 5
    Since this post was written commons-io FileUtils 2.1 has been updated to support appending to files. writeStringToFile(File file, String data, boolean append) – Mark McLaren Oct 24 '11 at 10:10
  • Vote for this answer: http://stackoverflow.com/a/8294642/11236 – ripper234 Dec 07 '11 at 10:17
  • Why don't you accept the answer by JJ Roman? It works with the latest version of Apache IO. – Daniil Shevelev Oct 16 '13 at 21:54
  • 1
    @DaSh I have marked it as accepted. – Dexter Oct 16 '13 at 22:06

7 Answers7

67

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);
JJ Roman
  • 4,225
  • 1
  • 27
  • 21
  • Thanks for the answer. +1 – Diganta Dec 16 '16 at 06:42
  • 2
    There should be another argument to `writeStringToFile()` in the 3rd position: the `Charset` to use. Current versions of Apache IO report the above mentioned method as deprecated. – vektor Jun 14 '17 at 04:49
5

Download the latest version Commons-io 2.1

FileUtils.writeStringToFile(File,Data,append)

set append to true....

om-nom-nom
  • 62,329
  • 13
  • 183
  • 228
Makky
  • 17,117
  • 17
  • 63
  • 86
4

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

}
thirtydot
  • 224,678
  • 48
  • 389
  • 349
Ryan
  • 1,171
  • 1
  • 10
  • 23
  • +1. And now, we just need a Java 7 style utility with the new resource management style: http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html ... and also - how the hell isn't this stuff included in Apache Commons or another library? – ripper234 Dec 07 '11 at 10:12
  • Ah, never mind, by now FileUtils actually do contain FileUtils.writeStringToFile(); Vote for http://stackoverflow.com/a/8294642/11236 – ripper234 Dec 07 '11 at 10:17
2

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

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
2

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>
gps
  • 317
  • 5
  • 10
2

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

FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);
Roushan
  • 4,074
  • 3
  • 21
  • 38
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
White_Sox
  • 61
  • 1
  • 1
  • 6