在写入新内容之前清除文件(Java、Android)

Ale*_*tau 3 java android

我使用以下代码创建一个新文件(二进制或 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)

如何在写入文件之前清理文件,或清除剩余内容?

Com*_*are 6

尝试在写入之前"wt"使用,w来截断文件 ( )。t