Android文件系统:java.io.FileNotFoundException:/ savedArticlesFile:open failed:EROFS(只读文件系统)

时间:2014-11-05 19:50:53

标签: java android io filenotfoundexception

即使在阅读了每个Stack Overflow问题之后我仍然会收到错误,我可以找到并尝试几种不同的创建文件的方法。我目前有以下两种方法来读取和写入一组对象到内部存储:

public HashSet<PrestoItem> readSavedArticleFile() {
    Log.d("snw", "readSavedArticlesFile()");
    String filename = "savedArticlesFile";
    String filePath = getActivity().getFilesDir().getPath().toString() + "/" + filename;
    File savedArticlesFile = new File(filePath);
    ObjectInputStream input;
    HashSet<PrestoItem> articleSet = null;

    // retrieve article set
    try {
        input = new ObjectInputStream(new FileInputStream(savedArticlesFile));
        articleSet = (HashSet<PrestoItem>) input.readObject();
        input.close();
    } catch (StreamCorruptedException e) {
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    if (articleSet == null) {
        articleSet = new HashSet<PrestoItem>();
    }

    return articleSet;
}

public void saveArticleSet(HashSet<PrestoItem> articleSet) {
    // write set back to file
    String filename = "savedArticlesFile";
    String filePath = getActivity().getFilesDir().getPath().toString() + "/" + filename;
    ObjectOutput output;
    try {
        output = new ObjectOutputStream(new FileOutputStream(filePath));
        output.writeObject(articleSet);
        output.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

每当我点击try块,即尝试打开输入或输出流时,我都会收到ROFS错误。我的印象是我提供的路径应该启用了r / w权限,但即使在读取时也会抛出错误。我显然在某个地方误解了某些东西。任何指导都将不胜感激。

编辑:堆栈跟踪

11-05 15:40:09.659  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ java.io.FileNotFoundException: /data/data/com.gannett.dolly.CooksCorner/files/savedArticlesFile: open failed: ENOENT (No such file or directory)
11-05 15:40:09.659  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
11-05 15:40:09.659  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.gannett.dolly_android.WebAppLoaderFragment.readSavedArticleFile(WebAppLoaderFragment.java:378)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.gannett.dolly_android.WebAppLoaderFragment$2.shouldOverrideUrlLoading(WebAppLoaderFragment.java:225)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.webview.chromium.WebViewContentsClientAdapter.shouldOverrideUrlLoading(WebViewContentsClientAdapter.java:293)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.org.chromium.android_webview.AwContentsClientBridge.shouldOverrideUrlLoading(AwContentsClientBridge.java:96)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.org.chromium.base.SystemMessageHandler.nativeDoRunLoopOnce(Native Method)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.org.chromium.base.SystemMessageHandler.handleMessage(SystemMessageHandler.java:27)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:102)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at android.os.Looper.loop(Looper.java:146)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5487)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at libcore.io.Posix.open(Native Method)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
11-05 15:40:09.669  26726-26726/com.gannett.dolly.CooksCorner W/System.err﹕ ... 15 more

1 个答案:

答案 0 :(得分:0)

尝试使用directory.mkdirs()代替directory.mkdir()