Java append to file

Java append to file tutorial shows how to append to a file in Java. We use FileWriter, FileOutputStream, Files, RandomAccessFile, Google Guava, and Apache Commons IO.

Appending to a file is often used in logging.

In the examples, we will append text to the files.

towns.txt
Bratislava
Moldava
Košice
Trenčín
Prešov
Žilina

We use this text file. It is located in src/main/resources directory.

Java append to file with FileWriter

FileWriter class is used for writing streams of characters. FileWriter takes an optional second parameter: append. If set to true, then the data will be written to the end of the file.

JavaAppendFileFileWriter.java
package com.zetcode;

import java.io.FileWriter;
import java.io.IOException;

public class JavaAppendFileFileWriter {
    
    public static void main(String[] args) throws IOException {
        
        String fileName = "src/main/resources/towns.txt";
        
        try (FileWriter fw = new FileWriter(fileName, true)) {
            
            fw.append("Žilina\n");
        }
    }
}

This example appends data to a file with FileWriter.

Java append to file with FileOutputStream

FileOutputStream is an output stream for writing data to a File or to a FileDescriptor. It takes an optional second parameter, which determines whether the data is appended to the file.

JavaAppendFileFileOutputStream.java
package com.zetcode;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class JavaAppendFileFileOutputStream {
    
    public static void main(String[] args) throws FileNotFoundException, IOException {
        
        String fileName = "src/main/resources/towns.txt";
        
        byte[] tb = "Žilina\n".getBytes();
        
        try (FileOutputStream fos = new FileOutputStream(fileName, true)) {
            fos.write(tb);
        }
    }
}

This example appends data to a file with FileOutputStream.

Java append to file with Files

Java 7 introduced java.nio.file.Files class, which can be used to easily append data to a file.

JavaAppendFileFiles.java
package com.zetcode;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;

public class JavaAppendFileFiles {
    
    public static void main(String[] args) throws IOException {
        
        String fileName = "src/main/resources/towns.txt";
        
        byte[] tb = "Žilina\n".getBytes();
        
        Files.write(Paths.get(fileName), tb, StandardOpenOption.APPEND);
    }
}

This example appends data with Files.

Files.write(Paths.get(fileName), tb, StandardOpenOption.APPEND);

The third parameter of Files.write() tells how the file was opened for writing. With the StandardOpenOption.APPEND, the file was opened for appending.

Java append to file with RandomAccessFile

RandomAccessFile is used for reading and writing to a random access file.

JavaAppendFileRandomAccessFile.java
package com.zetcode;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

public class JavaAppendFileRandomAccessFile {

    public static void main(String[] args) throws IOException {
        String fileName = "src/main/resources/towns.txt";

        File file = new File(fileName);

        try (RandomAccessFile raf = new RandomAccessFile(file, "rw")) {

            long length = raf.length();

            raf.setLength(length + 1);
            raf.seek(raf.length());
            raf.writeBytes("Žilina\n");
        }
    }
}

In this example we append data with RandomAccessFile.

Java append to file with Guava

We can use Guava library for appending to a file.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>22.0</version>
</dependency>

We need this Guava dependency.

JavaAppendFileGuava.java
package com.zetcode;

import com.google.common.base.Charsets;
import com.google.common.io.CharSink;
import com.google.common.io.FileWriteMode;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;

public class JavaAppendFileGuava {

    public static void main(String[] args) throws IOException {

        String fileName = "src/main/resources/towns.txt";

        File file = new File(fileName);

        CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);
        chs.write("Žilina\n");
    }
}

In the example, we append to a file with with Guava's CharSink class.

CharSink chs = Files.asCharSink(file, Charsets.UTF_8, FileWriteMode.APPEND);

The third parameter of Files.asCharSink() specifies the file writing mode; with FileWriteMode.APPEND option the file is opened for writing.

Java append to file with Apache Commons IO

In the final example, we use Apache Commons IO to append to a file.

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.5</version>
</dependency>

We need this dependency.

JavaAppendFileApacheCommons.java
package com.zetcode;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.io.FileUtils;

public class JavaAppendFileApacheCommons {

    public static void main(String[] args) throws IOException {

        String fileName = "src/main/resources/towns.txt";

        File file = new File(fileName);

        FileUtils.writeStringToFile(file, "Žilina", StandardCharsets.UTF_8, true);
    }
}

We append to the file with the FileUtils.writeStringToFile() method. The last append parameter determines whether to append to a file.

In this tutorial, we have shown how to append to a file in Java with built-in tools and third-party libraries.

You might also be interested in the following related tutorials: Java tutorial, Java list directory contents, Java FileWriter tutorial, Java file time, Joining strings with Java 8's StringJoiner, Introduction to Google Guava, Filtering a list in Java, or Android tutorial.