I opened a file, and every time I want to write to it, it saves what I wrote the last time. How do I delete all the contents of the file?.
My code:
String fileName = "MyFile";
String content = "Hello from file";
FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_APPEND);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
I know that Context.MODE_APPEND
is matters. What should i write insted?
String fileName = "MyFile.txt";
String content = "Hello from file";
try {
OutputStream out = new FileOutputStream(fileName, false);
out.write(content.getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
Just create your FileOutputStream like this
Every time you open a new FileOutputStream it will start fresh again. The old data will be overwritten / gone
The second argument of the FileOutputStream constructor is whether it should append or not. By setting it to false or passing no second argument at all it won't append