14

The Android Developer reference (this page) says:

Throws FileNotFoundException

But at the very start, it says:

Open a private file associated with this Context's application package for writing. Creates the file if it doesn't already exist.

If that is the case, why would the FileNotFoundException ever be thrown?

I just want to make sure I'm properly handling all cases. I am using the default functionality, so can I just wrap this in a try..catch block with nothing in the catch block since it is not possible for a FileNotFoundException to ever be thrown in the default functionality?

Edit: example of 'default functionality':

String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = context.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
2
  • Is it possible that your app will not have permission to create file in place you point?
    – Pshemo
    Nov 23, 2013 at 12:54
  • @Pshemo I should point out, I'm not actually getting the Exception, just preparing for the possibility it might be thrown on another device. But folder permission is a very valid point (though it should not be an issue on the root dir, right?)
    – 1owk3y
    Nov 23, 2013 at 12:57

4 Answers 4

12

This might happen for example if you try to open a Folder or if the file you try to open does not exist, but you don't have permissions to create it either.

4
  • This seems like the most likely situation where the Exception would be thrown; thank you for your answer
    – 1owk3y
    Nov 23, 2013 at 13:00
  • 4
    Makes sense, although an incredibly poor choice of exception name!
    – Rob Grant
    Mar 3, 2016 at 21:14
  • 1
    'Does not exist' is correct, but 'but you don't have permissions to create it either' is not. FileOutputStream does not attempt to create directories.
    – user207421
    Aug 19, 2020 at 9:38
  • 1
    why do not have permissions sometimes?
    – pain
    Jun 16, 2022 at 5:15
3

The ContextImpl.openFileOutput is implemented as following on ICS:

@Override
public FileOutputStream openFileOutput(String name, int mode)
    throws FileNotFoundException {
    final boolean append = (mode&MODE_APPEND) != 0;
    File f = makeFilename(getFilesDir(), name);
    try {
        FileOutputStream fos = new FileOutputStream(f, append);
        setFilePermissionsFromMode(f.getPath(), mode, 0);
        return fos;
    } catch (FileNotFoundException e) {
    }

    File parent = f.getParentFile();
    parent.mkdir();
    FileUtils.setPermissions(
        parent.getPath(),
        FileUtils.S_IRWXU|FileUtils.S_IRWXG|FileUtils.S_IXOTH,
        -1, -1);
    FileOutputStream fos = new FileOutputStream(f, append);
    setFilePermissionsFromMode(f.getPath(), mode, 0);
    return fos;
}

The function makeFileName will make sure you cannot specify any directory structure here:

private File makeFilename(File base, String name) {
        if (name.indexOf(File.separatorChar) < 0) {
            return new File(base, name);
        }
        throw new IllegalArgumentException(
                "File " + name + " contains a path separator");
    }

Although this seems not possibly to see the Fnf exception, however, you can see that it is not thread safe, so f.getParentFile() might still throw that exception if some other thread is removing the /data/data/com.yourpkg.name/files directory.

5
  • I see references to 'append mode', which is not default functionality, however thank you for illustrating a case where the Exception can be thrown
    – 1owk3y
    Nov 23, 2013 at 13:00
  • Is Boda's answer really the case you are talking about?
    – Robin
    Nov 23, 2013 at 13:03
  • Yes, since a missing source folder may also cause the issue, and his answer is the most likely and relevant for anyone else who raises this question. I take this opportunity however to thank you for the amount of depth and research you put into your answer
    – 1owk3y
    Nov 23, 2013 at 13:07
  • Curious that it bothers to create the parent directory but not the grandparent etc. directories.
    – user207421
    Nov 26, 2013 at 3:11
  • Android want to limit all your 'misc' files to be stored in the 'files' folder in your package directory.
    – Robin
    Nov 26, 2013 at 3:28
2

The most common cause is that an intermediate directory doesn't exist. FileOutputStream won't create those. It can also be a permissions problem: the entire path exists but you don't have create permissions in the final directory, or overwrite permissions on the actual file if it already exists.

2
  • 1
    You are talking on generic Linux, for this special Android 'files' folder case, the 'files' folder is normally created by framework APIs. So the situation mentioned in your answer will happen when user intentionally manual create the 'file' folder without assign the write permission?
    – Robin
    Nov 26, 2013 at 3:59
  • 1
    @Rohan I am talking about generic operating systems. The word 'Linux' does not appear in my answer, which applies equally to Unix, Windows, AIX, HP-UX, Solaris, MS.DOS, OS/2, IX/370, and any number of other systems. I've already addressed the matter of permissions in the final directory.
    – user207421
    Oct 7, 2015 at 20:44
2

In my case the reason was that the name of the file was not OK. Obviously, colons in the filename are disliked.

Not working (-> FileNotFoundException):

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss.SSS");
    String fileName = simpleDateFormat.format(new Date());
    FileOutputStream fileOutputStream = new FileOutputStream(new File(context.getFilesDir(), fileName), false);

Working:

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss.SSS");
    [...]

(Difference is the "." instead of ":" between HH, mm and ss in the SimpleDateFormat)

1
  • Thanks! "." instead of ":" is what I was looking for!
    – Inoy
    Mar 7, 2017 at 23:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.