android - 如何在不使用 FileOutputStream 的情况下使用 SAF 附加文件内容
问题描述
FileOutputStream
允许附加文件,只需在其构造函数中设置附加属性,不幸的是,由于直接路径限制,无法使用true
SAF,并且文件写入类似于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);
行为完全相同并且替换文件的内容,但我希望每次调用将新输入附加到文件的现有内容。
解决方案
有两种形式openOutputStream()
。您正在使用的单参数版本相当于调用模式为 的双参数版本"w"
。对于追加操作,请尝试使用"wa"
as 模式的双参数版本:
OutputStream os = resolver.openOutputStream(source_uri, "wa");
有关更多信息,请参阅文档。
推荐阅读
- java - 是否可以同时拥有 localhost:8080 和 www?还是 www 表示它是公开的?
- firebase - 调用 Firebase.instance.verifyPhoneNumber() 导致颤振应用程序(android)崩溃
- r - R - 创建具有二元预测的 ROC 曲线
- javascript - Discord.js v12 警告命令;Math.floor 错误
- c++ - QT 自定义小部件悬停
- alibaba-cloud - 阿里巴巴云上的公共 DNS (IPv4) 是否用于其他云?
- java - hibernate 5 org.hibernate.hql.internal.ast.QuerySyntaxException: Emplooye 未映射
- php - 带有 PhpMyAdmin 5.0.4 的 PHP 问题版本
- openssl - 如何检查/比较 openssl 速度
- sql - 替换序列化 PHP 对象 Bia 中的文本 PHPMYADMIN MariaDB SQL 查询