Append data into a file using Apache Commons I/O

前端 未结 7 1606
野的像风
野的像风 2020-12-09 14:48

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 th

相关标签:
7条回答
  • 2020-12-09 15:27

    Download the latest version Commons-io 2.1

    FileUtils.writeStringToFile(File,Data,append)
    

    set append to true....

    0 讨论(0)
  • 2020-12-09 15:29

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

    FileUtils.writeStringToFile(file, "line to append", "UTF-8", true);
    
    0 讨论(0)
  • 2020-12-09 15:38

    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

    0 讨论(0)
  • 2020-12-09 15:39

    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() {}
    
    }
    
    0 讨论(0)
  • 2020-12-09 15:42

    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>
    
    0 讨论(0)
  • 2020-12-09 15:49

    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);
    
    0 讨论(0)
提交回复
热议问题