Languages
[Edit]
EN

Java - append text to existing file

0 points
Created by:
Brian-Tompset
491

In this article, we would like to show you how to append text to an existing file in Java.

Below examples present how to append text into the file using the following solutions:

  • Files.write (append single line - Java 7, multiple lines - Java 8),
  • Files.writeString (Java 11),
  • FileWriter,
  • FileOutputStream.

 

1. Files.write

In the below examples, we use Files.write to append some text at the end of the existing example.txt file.

1.1 Append single line

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

public class Example {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("C:\\path\\example.txt");
        String string = "Dirask is awesome!\n";

        Files.write(path, string.getBytes(StandardCharsets.UTF_8),
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);
    }
}

The result after three executions:

Java - append text to an existing file using Files.write - result
Java - append text to an existing file using Files.write - result

1.2 Append multiple lines

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

public class Example {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("C:\\path\\example.txt");
        List<String> text = new ArrayList<>();

        text.add("line 1");
        text.add("line 2");
        text.add("line 3");

        Files.write(path, text,
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);
    }
}

The result after one execution:

Java - append text to an existing file using Files.write - result
Java - append text to an existing file using Files.write - result

2. Files.writeString

In the below examples, we use Files.writeString to append some text at the end of the existing example.txt file.

Note:

In Java 7 with Files.write() method, we need to convert a String into a byte[] and then write or append it to a file.

With Java 11 File.writeString() method we can directly write or append the string into the file. 

Practical example: 

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 Example {

    public static void main(String[] args) throws IOException {
        Path path = Paths.get("C:\\path\\example.txt");
        String text = "Dirask is awesome!";

        Files.writeString(path, text,
                StandardOpenOption.CREATE,
                StandardOpenOption.APPEND);
    }
}

Result:

Java - append text to an existing file using Files.writeString - result
Java - append text to an existing file using Files.writeString - result

3. FileWriter

In the below examples, we use FileWriter to append some text at the end of the existing example.txt file.

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class Example {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\path\\example.txt");
        List<String> text = new ArrayList<>();
        text.add("line 1");
        text.add("line 2");
        text.add("line 3");

        try (FileWriter fw = new FileWriter(file, true);
             BufferedWriter bw = new BufferedWriter(fw)) {

            for (String s : text) {
                bw.write(s);
                bw.newLine();
            }
        }
    }
}

The result after one execution:

Java - append text to an existing file using FileWriter - result
Java - append text to an existing file using FileWriter - result

4. FileOutputStream

In the below examples, we use FileWriter to append some text at the end of the existing example.txt file.

import java.io.*;
import java.nio.charset.StandardCharsets;

public class Example {

    public static void main(String[] args) throws IOException {
        File file = new File("C:\\path\\example.txt");
        String text = "Dirask is awesome!\n";

        try (FileOutputStream fos = new FileOutputStream(file, true)) {
            fos.write(text.getBytes(StandardCharsets.UTF_8));
        }
    }
}

Result:

Java - append text to an existing file using FileOutputStream - result
Java - append text to an existing file using FileOutputStream - result

Note:

Append mode of FileOutputStream works the same as FileWriter.

Alternative titles

  1. Java - append data to existing file
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Java - file operations

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join