【发布时间】:2016-10-14 08:07:03
【问题描述】:
将一些文本写入 android 设备上的文件似乎是一项重大努力。从an answer given here 开始,我在我的单个“Hello-World”活动中实现了以下代码:
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(this.openFileOutput("config.txt", Context.MODE_PRIVATE));
outputStreamWriter.write("lalala");
outputStreamWriter.close();
} catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
它不会抛出异常,但似乎可以工作。但是有没有办法在 android 上“查看”使用File Manager 创建的文件?代码 sn-p 似乎写入了与应用程序本身相关的 android 文件系统上的“秘密”位置(由 this.openFileOutput 管理)。
咨询不同的谷歌链接(thisand thisand this)我想出了以下代码:
File file = new File(this.getExternalFilesDir("temp"), "testfile.txt");
FileOutputStream fileOutput = openFileOutput(file.getName(), Context.MODE_WORLD_WRITEABLE);
fileOutput.write("lalala");
fileOutput.close();
这会引发错误
Error:(55, 19) error: no suitable method found for write(String) method FileOutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method FileOutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(int) is not applicable (actual argument String cannot be converted to int by method invocation conversion) method OutputStream.write(byte[],int,int) is not applicable (actual and formal argument lists differ in length) method OutputStream.write(byte[]) is not applicable (actual argument String cannot be converted to byte[] by method invocation conversion)
那么如何正确地做到这一点呢?
附带说明:这仅用于调试/教育目的,不打算成为最终应用程序的一部分!
要明确:我想在 temp 目录中创建一个文件,我可以使用 FileManager 看到(temp 目录与 My Documents 处于同一级别
、Music、DCIM、Download 等...)
【问题讨论】: