Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to append the written data to a file?

i am new developer in android.i would like to write some content to a file i have used a method to write into a file as follows

 public void writeFile(String path,String text){
    try{
    Writer output = null;
    File file = new File(path);
      output = new BufferedWriter(new FileWriter(file));
      output.write(text);
      output.close();
      System.out.println("Your file has been written");  
    }
    catch (Exception e) {
        e.printStackTrace();
    }

here i am passing path of a file and text to write.if i use in this way i can write the data but the previous data is losing.

how can i append or insert the latest text into a file without losing previous text?

Thanks in advance

like image 973
prasad.gai Avatar asked Jun 20 '11 05:06

prasad.gai


2 Answers

Try this. Change this line ...

output = new BufferedWriter(new FileWriter(file));

to

output = new BufferedWriter(new FileWriter(file, true));

The true indicates that you want to append not overwrite

like image 125
Sai Avatar answered Oct 05 '22 22:10

Sai


Have a look here and try:

new FileWriter(file, true);

the boolean indicates whether or not to append to an existing file.

like image 38
oliholz Avatar answered Oct 06 '22 00:10

oliholz