Problem description
This exception occurs when I/O is faulty. It can be caused by multiple reasons, such as nonexistent files, missing read and write permissions, encoding errors, EOF returned, I/O interruption, and invalid JSON and URL formats.
Solution
This exception can be caused by many reasons, such as nonexistent files, missing read and write permissions, encoding errors, EOF returned, I/O interruption, and invalid JSON and URL formats. We recommend that you catch the exception and handle it.
Sample code
Example 1
java.io.IOException: open failed: EACCES (Permission denied)
Check the file path and grant the permissions to read and write files on the external storage:
<uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Example 2
java.io.IOException: open failed: ENOENT (No such file or directory)
Add the try-catch statement to handle exceptions.
public void readFile(String filePath) {
BufferedReader br = null;
try {
String sCurrentLine;
br = new BufferedReader(new FileReader(filePath));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null)
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
References
https://developer.android.com/training/permissions/index.html (Permission usage documentation)
https://developer.android.com/reference/android/Manifest.permission.html (Permission classification documentation)
https://www.mkyong.com/java/how-to-read-file-from-java-bufferedreader-example/
https://developer.android.com/reference/java/io/IOException.html
https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous (Common and risky permissions)
https://developer.android.com/training/permissions/requesting.html#perm-check (Authorization methods)