0

Here's the code:

private String readFile(String dataFile)
{
    String myData = "";
    File myExternalFile = new File("assets/",dataFile);
    try {
        FileInputStream fis = new FileInputStream(myExternalFile);
        DataInputStream in = new DataInputStream(fis);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));

        String strLine;
        while ((strLine = br.readLine()) != null) {
            myData = myData + strLine + "\n";
        }
        br.close();
        in.close();
        fis.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return myData;
}

The file is here:

enter image description here

Here's what happens when I run the project: enter image description here

nicomp
  • 4,344
  • 4
  • 27
  • 60
  • @karan No. I'm asking why my code doesn't work. I already studied those answers. – nicomp Nov 27 '20 at 13:33
  • 1
    your code is looking for file in assets/ folder which is not the correct path. try using` properContext.getAssets().open("data.txt")` which also returns InputStream. – karan Nov 27 '20 at 13:49
  • 1
    You need to get the correct context through the Android Storage Access Framework (SAF) to avoid the direct standard programming file operations. Now, starting from API29+, you need to get familiar with the Scoped Storage concept which requires a context for any external folders and files in a sandboxed application environment. – ecle Nov 27 '20 at 13:58
  • Thanks to @ecle and karan for taking the time to explain what I did wrong. – nicomp Nov 27 '20 at 14:04

0 Answers0