【发布时间】:2013-12-08 08:54:48
【问题描述】:
Android 开发者参考 (this page) 说:
Throws FileNotFoundException
但一开始,它说:
打开与此上下文的应用程序包关联的私有文件以进行写入。 如果文件不存在,则创建该文件。
如果是这样,为什么会抛出 FileNotFoundException?
我只是想确保我能妥善处理所有案件。我正在使用默认功能,所以我可以将其包装在 try..catch 块中,而 catch 块中没有任何内容,因为不可能在默认功能中抛出 FileNotFoundException 吗?
编辑:“默认功能”示例:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
【问题讨论】:
-
您的应用是否可能无权在您指向的位置创建文件?
-
@Pshemo 我应该指出,我实际上并没有得到异常,只是为它可能被抛出到另一台设备上的可能性做准备。但是文件夹权限是一个非常有效的点(尽管它不应该是根目录的问题,对吧?)
标签: java android java-io android-file android-context