All Products
Search
Document Center

Mobile Testing:java_io_IOException

Last Updated:Feb 28, 2022

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