我使用以下代码创建一个新文件(二进制或 JSON)或重写 Android 中的现有文件:
ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(uri, "w");
FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor());
fileOutputStream.write(bytes);
fileOutputStream.close();
pfd.close();
Run Code Online (Sandbox Code Playgroud)
问题是当文件已经存在并且其内容大小较大时,旧内容仍然存在。例如 JSON 可以看起来像
[
"a",
"b"
] "c",
"d"
]
Run Code Online (Sandbox Code Playgroud)
如何在写入文件之前清理文件,或清除剩余内容?
| 归档时间: |
|
| 查看次数: |
1030 次 |
| 最近记录: |