I want to open/create a document file and rewrite it completely if it exists.
Using How to rewrite DocumentFile does not help (at least in API 32). In fact,
OutputStream os = getContentResolver().openOutputStream(documentFile.getUri(), "w");
does not clear the rest of the file if the new stream is shorter.
Any way to resolve it?
Additional nuisance, DocumentFile paradigm seems incompatible with the regular Java File. There you can open a File, then check if it exists. On Android, if the doc file "name" exists, it creates "name (1)", "name (2)", etc. automatically, so it always exists.
Why??
Asked
Active
Viewed 77 times
0

Alex B
- 347
- 1
- 3
- 9
1 Answers
0
Truncate by "wt".
If you use the same uri again then it will overwrite the same file.
Do not blindly call createFile.
First use findFile and if you get an uri then use that to overwrite.

blackapps
- 8,011
- 2
- 11
- 25
-
ok, "wt" works, though I do not understand why text mode makes the difference. I understand that findFile sort of solves the second issue. My point is that Java File works differently and documentFile.exists() is useless. – Alex B Sep 18 '22 at 21:04
-
What do you mean with text mode? t== truncate. – blackapps Sep 19 '22 at 05:20
-
`sort of` ????? – blackapps Sep 19 '22 at 05:21
-
"sort of" means "Java File works differently and documentFile.exists() is useless" – Alex B Sep 19 '22 at 16:19