将一些文本写入Android设备上的文件似乎是一项重大努力.从这里给出的答案开始,我已将以下代码实现到我的单个"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());
}
这不会引发异常,但似乎有效.但有没有办法"看到"使用File Manageron android 创建的文件?代码片段似乎写入android文件系统上与应用程序本身相关的"秘密"位置(由using控制this.openFileOutput).
咨询不同的谷歌链接(这个 和这个 和这个)我想出以下代码:
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目录,我可以用文件管理器看到(该temp目录是在同一水平My Documents
,Music,DCIM,Download等...)