简体   繁体   中英

Can't read text file from the data folder or documents folder in the phone's internal storage

I am trying to read a hi.txt file present in the "Data" folder inside the phone's internal storage. I tried the following:

try{
File dir = Environment.getDataDirectory();
File file = new File(dir, "hi.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
     String line;
     line = br.readLine());
}catch (Exception e) {
System.out.println(e.getMessage());
}

But, I get the exception: "/data/hi.txt" (no such file or directory)

I am sure the hi.txt file is there!

Same thing happens if I try opening it from the documents folder also in internal storage like this:

File dir= new File("/Documents/hi.txt");

What am I doing wrong? And how can I read a text file that is stored anywhere in the internal storage? like in the DCIM folder or the Documents folder or Movies or Pictures or Music ..etc ? I need to use a public text file that can be accessed by any application on the phone.

As I can't add a comment, I have to use answer. Have you tried with Environment.getDataDirectory().getAbsolutePath() , as you are right about the permissions in Manifest, you need them just for external storage. And the other part of the code looks well to me.

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