为什么FileOutputStream抛出FileNotFoundException?

1ow*_*k3y 9 java android android-context java-io android-file

Android Developer参考(本页)说:

Throws FileNotFoundException
Run Code Online (Sandbox Code Playgroud)

但在一开始,它说:

打开与此Context的应用程序包关联的私有文件以进行写入.如果文件尚不存在,则创建该文件.

如果是这种情况,为什么会抛出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();
Run Code Online (Sandbox Code Playgroud)

Blu*_*lub 9

例如,如果您尝试打开文件夹或者您尝试打开的文件不存在,但您没有创建它的权限,则可能会发生这种情况.

  • 有道理,尽管异常名称的选择非常糟糕! (3认同)