I'm using the following code to create a new file (binary or JSON) or rewrite an existing one in Android:
ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
fileOutputStream.write(bytes);
fileOutputStream.close();
pfd.close();
The problem is when the file already exists and its content size is larger, the old content remains. E.g. JSON can look like
[
"a",
"b"
] "c",
"d"
]
How do I clean the file before writing into it, or clear the remaining content?