当前位置: 首页 > 知识库问答 >
问题:

fileNotFoundException,打开失败:ENOENT

岳研
2023-03-14

出于某种原因,我在两次阅读中都获得了FileNotFoundException。值得注意的是Toast会打印“文件存在!”。我使用底部的bufferedreader来测试文件的内容是否正确。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_calendar, container, false);

    recipes = new ArrayMap<>();
    filename = "calendar_recipes.txt";

    bText= (EditText) v.findViewById(R.id.bEditText);
    lText= (EditText) v.findViewById(R.id.lEditText);
    dText= (EditText) v.findViewById(R.id.dEditText);

    cal = (CalendarView) v.findViewById(R.id.calendarView);
    date = cal.getDate();


    File file = getActivity().getFileStreamPath(filename);

    if(file.exists())
    {
        Toast.makeText(getActivity(), "File exists!", Toast.LENGTH_SHORT).show();
        try
        {
            FileInputStream fileInputStream = new FileInputStream(getActivity().getFilesDir()+filename);
            ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
            Map recipes = (Map)objectInputStream.readObject();
        }
        catch(ClassNotFoundException | IOException | ClassCastException e) {
            e.printStackTrace();
        }
    }
    else
    {
        Toast.makeText(getActivity(), "File does not exist!!", Toast.LENGTH_SHORT).show();
        file = new File(getActivity().getFilesDir(), filename);
    }

    try {
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String line;
        while ((line = in.readLine()) != null) {
            System.out.println(line);
        }
        in.close();
    }

    catch (IOException e) {
        e.printStackTrace();
    }

Logcat...

03-24 23:54:57.626 14059-14067/com.stringcheese.recipez.recip_ez W/art: Suspending all threads took: 7.202ms
03-24 23:54:58.409 14059-14059/com.stringcheese.recipez.recip_ez W/System.err: java.io.FileNotFoundException: /data/data/com.stringcheese.recipez.recip_ez/filescalendar_recipes.txt: open failed: ENOENT (No such file or directory)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:456)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:76)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:103)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at com.stringcheese.recipez.recip_ez.CalendarFragment.onCreateView(CalendarFragment.java:80)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:339)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:602)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1259)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.Activity.performStart(Activity.java:6026)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2302)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.ActivityThread.access$800(ActivityThread.java:155)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.os.Looper.loop(Looper.java:135)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5343)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
03-24 23:54:58.410 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at java.lang.reflect.Method.invoke(Method.java:372)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at libcore.io.Posix.open(Native Method)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:     at libcore.io.IoBridge.open(IoBridge.java:442)
03-24 23:54:58.411 14059-14059/com.stringcheese.recipez.recip_ez W/System.err:  ... 23 more

共有1个答案

楚昊明
2023-03-14

getFilesDir返回文件对象。如果对它调用onString(隐式调用),它将返回路径。如果文件是目录,则路径不以斜杠结尾,因此getactivity().getFilesDir()+filename将导致类似于“/data/data/com.yourapp/filescalendar_recipes.txt”的结果。

您可以使用getActivity().getFilesDir()+file.separator+filename,或者只调用new FileInputStream(file)

 类似资料:
  • 在其他设备中没有发生这种情况,我测试的设备:、、

  • null 我在用这个代码 但我一直得到这个错误

  • 问题内容: 由于某种原因,我两次阅读都得到了一个。值得注意的是,Toast会打印“文件存在!”。我使用底部的来测试文件的内容是否正确。 Logcat … 问题答案: 返回一个File对象。如果在其上调用onString(您暗中进行了此操作),它将返回其路径。如果该文件是目录,则该路径 不会 以斜杠结尾,因此将导致类似的内容。 您可以使用,也可以仅致电。

  • 嗨,我正在一台机器上部署一个我写的应用程序(所有的部署都是用tomcat)。 当运行应用程序(在浏览器中)时,我从catalina.out中得到一个错误: 会有什么问题?

  • 当使用意图选择图像时,我会得到一个无法打开文件的URI。如果我尝试打开文件,如下所示: 它给出了以下例外情况: <代码>/文档/图像: 9537 似乎确实是一条不正确的路径,但是我如何获得正确的路径呢? 我用这个逻辑打开图像拾取器: 然后在onActivityResult中检索Uri,如下所示: 我需要对文件进行解码,使其变小。

  • 我们最近在尝试访问Unix服务器中的weblogic控制台时遇到了一个问题,我们将得到: 错误503--服务不可用 在日志文件中,它会指出: 原因:java。io。FileNotFoundException:/opt/weblogic1036/wlserver\u 10.3/server/lib/co 在谷歌上搜索了一番后,我找到了一个解决方案,其中指出/etc/security/limits。c