代码之家  ›  专栏  ›  技术社区  ›  D. Rao

java.io。FileNotFoundException,打开失败:ENOENT

  •  3
  • D. Rao  · 技术社区  · 10 年前

    出于某种原因,我得到了 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
    
    5 回复  |  直到 10 年前
        1
  •  8
  •   F43nd1r    10 年前

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

    您可以使用 getActivity().getFilesDir()+File.separator+filename ,或直接打电话 new FileInputStream(file) .

        2
  •  3
  •   Mohd Zaid    6 年前

    我也犯了同样的错误。 This 答案对我很有用。

    您只需在清单中添加一行,如下所示:

    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:requestLegacyExternalStorage="true" //Add this Line
    android:label="@string/app_name">
    

    -------------------

        3
  •  0
  •   LuisFMorales    10 年前

    您已经创建了 File 带有的实例 File file = getActivity().getFileStreamPath(filename); 这是您正在检查的实例 file.exists() 方法然后,您尝试用 FileInputStream .你应该试试 FileInputStream fileInputStream = new FileInputStream(file); 。这样,您将使用已检查的文件创建流。

        4
  •  0
  •   صلي علي محمد - Atef Farouk    6 年前

    更新版本的android有时不支持创建文件夹(对我来说很奇怪,但我经历过),然后:-

    1-确保已创建文件夹,或/ 2-将此添加到mainfest <application android:requestLegacyExternalStorage="true" tools:targetApi="q">

        5
  •  0
  •   madlymad    5 年前
    public class Printer {
        private String name;
    
        public void setName(String name) {
            this.name = name;
        }
    
        public void print() {
            printString(name);
        }
    
        private void printString(String s) {
            System.out.println(s + " (" + s.length() + ")");
        }
    
        public static void main(String[] args) {
            Printer printer = new Printer();
            printer.print();
        }
    }