why sometime it throws FileNotFoundException why sometime it throws FileNotFoundException android android

why sometime it throws FileNotFoundException


I can think of the following possible reasons for this behavior:

  1. The new file could not be created simply because there is no free space on SD card. The next time you encounter this issue, try to see if you have at least some available space via file manager or Settings -> Storage. I doubt you can do something about it if this is what happens on your user's device.
  2. The SD card is not available due to some OS glitch or it's physically not attached, thus the file could not be created. Once again - there is nothing you can do about it, other than showing some Toast with error message.
  3. How do you generate the fileName part of your file's path? Sometimes the files can not be created because the filename contains unsupported (by this particular device) characters. E.g. I have HTC Desire X with Android 4.1.2 which can not create files with the code similar to yours if the filename contains colon. Try another way to generate the filename and see if it makes a difference.
  4. Did you make sure that the Downloads directory exists before creating the file inside it? Write something like the following:

    if (!downloadDirectory.exists()) {    downloadDirectory.mkdirs();}
  5. The WRITE_EXTERNAL_STORAGE permission is considered dangerous on the Android 6, thus it can be revoked by the user at any time. Make sure user didn't revoke this permission before writing to the file.
  6. Always close the I/O stream when you finished working with it. Add the finally clause to your try-catch block and close both in and out streams in it:

    finally {    if (in != null) {        try {            in.close();        } catch (Exception e) { }    }    if (out != null) {        try {            out.close();        } catch (Exception e) { }    }}
  7. The last one - and finally I'm out of options. :) I suppose this issue could also arise if you write to the same file from multiple threads. If that's the case, try to synchronize access to your file-writing code.


According to Java docs - Class FileNotFoundException:

public class FileNotFoundException extends IOException

Signals that an attempt to open the file denoted by a specified pathname has failed.

This exception will be thrown by the FileInputStream, FileOutputStream, and RandomAccessFile constructors when a file with the specified pathname does not exist. It will also be thrown by these constructors if the file does exist but for some reason is inaccessible, for example when an attempt is made to open a read-only file for writing.

To resume, there are three cases where a FileNotFoundException may be thrown.

  1. The named file does not exist.
  2. The named file is actually a directory.
  3. The named file cannot be opened for reading for some reason.

got "file not exist" exception for the newly created file.

In your code I don't see two things

  1. Removing file after successful write. I'm not kidding at all. If I had a class like this and that error, I would make a kind of test, which would finish with deleting downlaodDirectory after copying content from source file. In that case it would pass the method or you would get another, but more specific error - like in my words: unable to delete file. Under use it
  2. Closing file after writing data. I don't see also a method to close files. In your code, does seem to be that your File is still open, so you run again an app and you get this unpredicted by you error.

To fix the issue just close both files after processing data using file.close() method.

Hope it help


  1. The issue might happen due to the way you create file:

    String downloadPath = downloadDirectory.getPath();String newFilePath = (downloadPath + "/" + fileName);File newFile = new File(newFilePath);

    The dangerous code is here:

    String newFilePath = (downloadPath + "/" + fileName);

    Instead, try to use the following approach:

    File newFile = new File(downloadDirectory, fileName);
  2. SD card availability - check the following documentationhttp://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage.It might be that media is not mounted (for instance on some old devicesit happens when user connects device to computer via usb) So, try tocheck if media mounted:

    /* Checks if external storage is available for read and write */public boolean isExternalStorageWritable() {     String state = Environment.getExternalStorageState();     if (Environment.MEDIA_MOUNTED.equals(state)) {         return true;     }     return false; }

    and notify user if not.