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
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
Have a look here and try:
new FileWriter(file, true);
the boolean indicates whether or not to append to an existing file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With