简体   繁体   中英

Android - java.io.FileNotFoundException: /storage/emulated/0/Notes/File.txt: open failed: ENOENT (No such file or directory)

I found lot of topics with the same problem, but they couldn't fix mine. I initially write a file as follow:`

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
    if (!root.exists()) {
        root.mkdirs();
    }
    File notefile = new File(root, sFileName);
    FileWriter writer = null;
    try {
        writer = new FileWriter(notefile);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.append(sBody);
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.flush();
    } catch (IOException e1) {
        e1.printStackTrace();
    }
    try {
        writer.close();
    } catch (IOException e1) {
        e1.printStackTrace();
    }

Don't worry about the try and catch blocks, i will clear them later :D. And this is the reader which should works in the same directory ("Notes" of the sdcard, if it doesn't exist, will be created), read the file, and put it on a Notify as you can see:`

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
    if (!root.exists()) {
        root.mkdirs();
    }

    File file = new File(root, "Nota.txt");


    //Read text from file
    text = new StringBuilder();

    try {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        while ((line = br.readLine()) != null) {
            text.append(line);
            text.append('\n');
        }
        br.close() ;
    }catch (IOException e) {
        e.printStackTrace();
    }

I really don't understand why i get this problem, i even try with

getExternalStorageDirectory().getAbsolutePath() 

but without success.

Can someone help me?

You've tried to check with a debugger if root exists when you do:

File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
    root.mkdirs();
}

I do not think it will create the folder when you write the file

To write a file in external storage, you need to have WRITE_EXTERNAL_STORAGE permission enabled.

Why are you trying to write a file in external storage?

I mean if you want this file for your app only means, use context.getExternalDirs() to get your app's sandbox, it doesn't require write permission, above android 4.2(Jelly bean).

If you want to share the file to other apps, you're doing the right job.

And before writing the file, check whether external storage is mounted programmatically.

Problem Solved

I used SharedPreferences of Android. I stored the data in MainActivity and take in in my Class as follow:

  • MainActivity

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = prefs.edit(); editor.putString("string_id", InputString); //InputString: from the EditText editor.commit();
  • In my Class to get my data

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); String data = prefs.getString("string_id", "no id"); //no id: default value

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM