FileNotFoundException: /storage/emulated/0/Android

浪子不回头ぞ 提交于 2020-08-19 10:51:20

问题


I try this file writer/reader code segment for test:

File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(("test").getBytes());
outputStream.close();

File file = new File(getExternalFilesDir(null), "LM/lm_lisdat_01.txt");
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));

In the 4. row i got this error message below but the "lm_lisdat_01.txt" file was created in LM directory:

java.io.FileNotFoundException: /storage/emulated/0/Android/data/hu.abisoft.lm/files/LM/lm_lisdat_01.txt: open failed: ENOENT (No such file or directory)

Can help anyone for answer this (i think simple) question? I'm newby in Android. Thank you!


回答1:


You are creating the file in one directory and trying to open it for input in another.

Environment.getExternalStorageDirectory() is /storage/emulated/0

getExternalFilesDir(null) is /storage/emulated/0/Android/data/hu.abisoft.lm/files

Use the same directory for file creation and input.




回答2:


With sdk, you can't write to the root of internal storage. This cause your error. Edit :

Based on your code, to use internal storage with sdk:

final File dir = new File(context.getFilesDir() + "/nfs/guille/groce/users/nicholsk/workspace3/SQLTest");
dir.mkdirs(); //create folders where write files
final File file = new File(dir, "BlockForTest.txt");



回答3:


Please see changes. Your path was wrong.

And also check whether file exists or not.

    File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");
    FileOutputStream outputStream = new FileOutputStream(file);
    outputStream.write(("test").getBytes());
    outputStream.close();

    File file = new File(Environment.getExternalStorageDirectory(), "LM/lm_lisdat_01.txt");//changes here
if(file.exists())
   { 

  BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
   }


来源:https://stackoverflow.com/questions/31063216/filenotfoundexception-storage-emulated-0-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!