0

Here is the code for writing a string to a text file.

try {
    OutputStreamWriter out=new OutputStreamWriter(openFileOutput("counts.txt", MODE_APPEND));
     try {
         s = main_text.getText().toString();
         out.write(s);
     }
     catch (java.io.IOException e) {
     }

Now how to delete all the contents of a text file without deleting the file itself.

Actually when ever adding the next string to a file it appends next to the previous string. What is required is to over write the previous string.

ligi
  • 39,001
  • 44
  • 144
  • 244
Usman Ahmed
  • 79
  • 2
  • 3
  • 11

2 Answers2

0

you need to use another mode - when you use MODE_APPEND it will append ;-) I think you should be fine with just changing MODE_APPEND -> 0

ligi
  • 39,001
  • 44
  • 144
  • 244
0

open file and try to write blank string into file.

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();
jeet parmar
  • 868
  • 8
  • 19