How to write in a text file concatenating with existing text in Java?

Asked

Viewed 2,721 times

3

I would like to write in a text file using Java, but whenever I use the functions write of BufferedWriter the previous text is deleted and the new one is written, I would like to keep the old text and just concatenate the new one.

2 answers

2


Add text to existing text you need to open the file with the mode APPEND. You can inform this in the Filewriter. FileWriter(String fileName, boolean append)

Example:

Writer arquivo = new BufferedWriter(new FileWriter("arquivo", true));
arquivo.append("Mais conteudo");
arquivo.close();

Vi here.

1

Before building the BufferedWriter, create a FileWriter with the parameter append of builder with the value true:

new BufferedWriter(new FileWriter("foo.out", true));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.