26

This is my noob question for the week. I'm looking more for general speculation than specific code and maybe hoping the Android folks are watching and could correct this:

the SDK documentation for Context.openFileOutput says:

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

Ok, that sounds good. I can create a file. Except this method also throws a FileNotFoundException, so apparently something is amiss. Why would a function that is supposed to create a file if it's not found throw an exception if the file is not found???

Kinda defeats that whole "Creates the file..." thing, doesn't it?

Vic
  • 5,977
  • 2
  • 22
  • 19
  • What path are you providing to `openFileOutput()`? – CommonsWare Feb 17 '11 at 21:00
  • 2
    can you run a debugger and check what is the detailMessage of your FileNotFoundException? you cannot use file separators in the method call (its stated in the docs, and in that case detailMessage is "File [...] contains a path separator"). – guido Feb 17 '11 at 21:55

4 Answers4

29

I have to apologize for leaping before I looked on this one. I kinda panicked while reading the documentation. After some testing, I found that openFileOutput() does, in fact, work as advertised and will create a file if it's not found, not just throw an FnF exception as I feared. Apparently, the FnF throw was added in case the Activity's application directory does not exist.

Again, my apologies but hopefully, this might help others who are confused by the documentation.

Vic
  • 5,977
  • 2
  • 22
  • 19
  • 2
    FileNotFoundException will also be thrown if there is a permissions problem that prevents the file from being created/opened. – Matt Connolly May 23 '11 at 21:22
  • 4
    @MattConnolly Then why is it called a "FileNotFoundException" ? – Pacerier Mar 05 '12 at 06:31
  • 2
    `openFileOutput` is declared as throwing only that exception. Beats me why they choose not to throw a more appropriate exception in other circumstances. http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String,%20int) – Matt Connolly Mar 06 '12 at 03:25
  • Another problems that I have right now, with Private file creation mode, it throws a Mono.Android exception "Operation is not supported" when the file doesn't exists. Have you ever encounter this? – ForceMagic Jul 29 '13 at 15:12
  • I believe a `FileNotFoundException` will also be thrown if a directory exists with the same name, making it impossible to create the file. And yes, the name is misleading. – pimmhogeling Aug 16 '14 at 22:58
1

FileNotFoundException is an exception thrown in case that you are trying to write to a file that does not exist, or cannot be currently accessed. When else would this occur?

  • Perhaps you forgot to close the file, and tried to open the same file.
  • Perhaps you tried to create multiple FileOutputStream objects pointing to the same file.

These will result in a FileNotFoundException.

Anyway, you can insert a throws FileNotFoundException at the end of your function declaration where you call openFileOutput (and to other functions that call this function).

f20k
  • 3,106
  • 3
  • 23
  • 32
1

Possibly also thrown if you use MODE_APPEND, which appends to an existing file and the file doesn't exist.

Jay
  • 141
  • 2
  • 5
-4

Have you inserted the right Permission? see http://developer.android.com/reference/android/Manifest.permission.html for reference. i think

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

could be the one You are fine with

Rafael T
  • 15,401
  • 15
  • 83
  • 144