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
+3
A:
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();
Adamski
2010-06-01 16:34:23
can you please provide me full code including import statements etc because i am having problem in understanding and running this code. thanks
sadia
2010-06-01 16:53:53
import java.io.*;
Jin Kim
2010-06-01 16:59:39
no sir i asked for the complete code. not only the import statement.
sadia
2010-06-01 17:04:18
this error is shown in my program when i compile it:cannot find symbolsymbol : constructor FileWriter(boolean,boolean)location: class java.io.FileWriter PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(file, true))); 1 error
sadia
2010-06-01 17:16:12
The compiler error suggests you are passing in two booleans rather than a File parameter and a boolean.
Adamski
2010-06-01 17:19:27