sadia
sadia

Reputation: 2023

how to use append function in file

I want to write in a file but in a way that it should not delete existing data in that file rather it should append that file. Can anybody please help by giving any example related to appending a file? Thank you

Upvotes: 1

Views: 795

Answers (1)

Adamski
Adamski

Reputation: 54705

You should use the FileWriter(File file, boolean append) constructor with the boolean value true.

Example

File file = new File("c:/tmp/foo.txt");
PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
pw.println("Hello, World");
pw.close();

Upvotes: 3

Related Questions