java.io.FileNotFoundException, open failed: ENOENT
Asked Answered
R

5

4

For some reason I am getting a fileNotFoundException for both the times I read. Something worth noting is that the Toast prints "File exists!". I used the BufferedReader at the bottom to test if the content of the file is correct.

@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
Rutledge answered 25/3, 2016 at 4:0 Comment(7)
Please show the full exceptionDrabeck
any reason you are not invoking the FileInputStream constructor on the same file object you call exists on?Sideman
Do you have this in your manifest? <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />Sideman
Check value of file.canRead()Sideman
I saw /data/data/com.stringcheese.recipez.recip_ez/filescalendar_recipes.txt, which I supposed it should be /data/data/com.stringcheese.recipez.recip_ez/files/calendar_recipes.txt. So you just need to add a slash in the text. I suggest you write an Utility class with a static function that take in a string as filename, and return object FileDrabeck
Yes that fixed the problem. Did not mean to get it filename. Thanks!Rutledge
Check this: https://mcmap.net/q/259688/-error-open-failed-enoent-no-such-file-or-directoryGapin
F
8

getFilesDir returns a File object. If you call onString on it (which you do implicitly), it returns its path. The path is not ending with a slash if the file is a directory, so getActivity().getFilesDir()+filename will result in something like "/data/data/com.yourapp/filescalendar_recipes.txt".

You can either use getActivity().getFilesDir()+File.separator+filename, or just call new FileInputStream(file).

Fayola answered 25/3, 2016 at 4:24 Comment(0)
F
3

I was Getting the same error. This Answer worked for me.

You just have to add one line in your manifest like this:

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

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

Frug answered 11/5, 2020 at 2:28 Comment(1)
Got this answer after waste of all my day . Thanks man.Berneta
V
0

You have already created a File instance with File file = getActivity().getFileStreamPath(filename); which is the instance that you are checking with the file.exists() method. Then you are trying to read another thing with the FileInputStream. You should try FileInputStream fileInputStream = new FileInputStream(file);. With that you are creating your stream with the file that you already checked.

Vagarious answered 25/3, 2016 at 4:27 Comment(0)
K
0

Newer version of android sometimes don't support creating folders ( very strange for me, but I experienced it), then :-

1- be sure that folder is created and or/ 2- add this to mainfests <application android:requestLegacyExternalStorage="true" tools:targetApi="q">

Killough answered 25/6, 2020 at 12:12 Comment(0)
R
0
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();
    }
}
Richy answered 10/7, 2021 at 5:57 Comment(1)
Pls fix it :( plsRichy

© 2022 - 2024 — McMap. All rights reserved.