Java IO - Append content to file

October 27, 2019 No comments Java IO Append File

1. Introduction

In this article, we will show how to append content to the existing file in Java. We are going to use BufferedWritter, PrintWriter, FileOutputStream and Files class available in Java IO. In Java appending methods are very similar to those responsible for writing the difference is in parameters.

2. Append to a file using BufferedWritter

BufferedWriter class can be used to append the content to an existing file. To append content add true parameter to the FileWriter constructor, like in the following example:

package com.frontbackend.java.io.append;

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

public class AppendToFileUsingBufferedWriter {

    public static void main(String[] args) {

        try (BufferedWriter writer = new BufferedWriter(new FileWriter("/tmp/test1.txt", true))) {
            writer.newLine();
            writer.write("FrontBackEnd.com"); // text to append

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

3. Append to a file using PrintWriter

Use PrintWriter to append formatted text to the file. PrintWriter comes with several methods to append any primitive types and objects. To activate append mode add second parameter to the FileWriter constructor.

package com.frontbackend.java.io.append;

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

public class AppendToFileUsingPrintWriter {

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

        final String textToAppend = "FrontBackend";

        FileWriter fileWriter = new FileWriter("/tmp/test3.txt", true); // true for append mode
        PrintWriter printWriter = new PrintWriter(fileWriter);
        printWriter.println(textToAppend);  // append new line
        printWriter.close();
    }
}

4. Append to a file using FileOutputStream

FileOutputStream responsible for writing binary data to the file. In this case to turn on append mode we need to add true parameter to the constructor.

package com.frontbackend.java.io.append;

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

public class AppendToFileUsingFileOutputStream {

    public static void main(String[] args)  {
        final String content = "FrontBackEnd.com";

        try (FileOutputStream outputStream = new FileOutputStream("/tmp/test2.txt", true)) {
            byte[] strToBytes = content.getBytes();
            outputStream.write(strToBytes);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

5. Append to a file using Files

To append content using Files class we must set third parameter for write method to StandardOpenOption.APPEND.

package com.frontbackend.java.io.append;

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

public class AppendToFileUsingFiles {

    public static void main(String[] args) throws IOException {
        final String textToAppend = "FrontBackend";

        Path path = Paths.get("/tmp/test4.txt");

        Files.write(path, textToAppend.getBytes(), StandardOpenOption.APPEND);  // activate append mode
    }
}

6. Conclusion

This article presented several methods to append content to the existing files in Java. Appending is very similar to writing to the file, the only difference is an additional parameter that tells we want to add content to the end of the file, not replacing it.

Like usual the following code snippets can be found in our GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}