0

How to read a .txt file from sdcard.I tried the following method but it returns empty string saying file not found exception


public String ReadFile(){

        File sdcard = Environment.getExternalStorageDirectory();
        File file = new File(sdcard,"/folder/json.txt");
        StringBuilder 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) {
            //You'll need to add proper error handling here
            Toast.makeText(this, "No data exist", Toast.LENGTH_SHORT).show();
        }

        String flag=text.toString();
        return flag;
    }

ERROR:/storage/emulated/0/folder/json.txt: open failed: ENOENT (No such file or directory)

Muhammed Riyas A V
  • 71
  • 1
  • 1
  • 11

1 Answers1

0

This is tips from me :

  1. Make sure the file is really exist, show the print out file URI.

  2. Make sure the read external permission is already added on Manifest file.

  3. Run on the real device, not on Emulator.

AFin
  • 61
  • 2