首页 > 解决方案 > 如何在不使用 FileOutputStream 的情况下使用 SAF 附加文件内容

问题描述

FileOutputStream允许附加文件,只需在其构造函数中设置附加属性,不幸的是,由于直接路径限制,无法使用trueSAF,并且文件写入类似于FileOutputStream

resolver = getContentResolver();
OutputStream os = resolver.openOutputStream(source_uri);
PrintWriter out = new PrintWriter(new OutputStreamWriter(os));
out.print(somestring);
out.flush();
out.close();

如果我使用out.append(somestring);而不是out.print(somestring);行为完全相同并且替换文件的内容,但我希望每次调用将新输入附加到文件的现有内容。

标签: androidstorage-access-framework

解决方案


有两种形式openOutputStream()。您正在使用的单参数版本相当于调用模式为 的双参数版本"w"。对于追加操作,请尝试使用"wa"as 模式的双参数版本:

OutputStream os = resolver.openOutputStream(source_uri, "wa");

有关更多信息,请参阅文档


推荐阅读