0

I would like to add more text to an existing text file in Java? When i try to add more text it just replaces the existing text.

mickey88
  • 9
  • 2

1 Answers1

0

This will append mycontent to the outputFile:

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

public class Test {

    public static void main(String[] args) {
        try {
            String content = "mycontent";
            File outputFile = new File("PATH/test.txt");
            Files.write(outputFile.toPath(), content.getBytes(), StandardOpenOption.APPEND);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Available since Java 1.7.

flavio.donze
  • 7,432
  • 9
  • 58
  • 91