The usual thing is to use atomic file open operations:
For input: Context.openFileInput()
For output: Context.openFileOutput()
Both of these either throw an exception when something goes wrong, or
return a File {Input|Output} Stream that you can read from /write to.
Same with "plain" Java: FileInputStream and FileOutputStream
consturctors either give you a file stream to work with, or throw an
exception.
Making a separate file existence check for leaves a potential hole for
other processes to intervene between the check and the actual file open
operation. Less so in Android, since every application has its own
private data directory (except if we're talking about external storage).
What happens when someone tries to delete a file that's open in another
application is OS dependent. Again, in Android, this is less of an issue
as applications in general should close open files when the user
switches away.
-- Kostya
18.01.2011 18:52, Paul пишет:
I agree, that would be by definition an exception state, where the
normal flow (check if file exists -> write to it) has been disrupted.
I have a little file name generation routine that does make use of the
FileNotFoundException though, out of pure convenience: (pseudo-code)
while (true) {
fileName = ...
try {
openFileInput(fileName);
} catch (FileNotFoundException e) {
// file doesn't exist, use this name
break;
}
}
Based on the top-rated answer here, re: staying in proper application
context:
http://stackoverflow.com/questions/2786655/test-if-file-exists
Paul
On Jan 18, 7:41 am, Kumar Bibek<coomar....@gmail.com> wrote:
Well, When someone deletes the file after you have created, and then you try
to write into it, then the catch block comes into picture. But, I guess,
that might not happen since the file would be locked. I am not sure though.
But just in case, that happens, don't you think, it's actually an EXCEPTION?
While trying to write into a file without checking if that file exists or
not is ideal I would say.
Kumar Bibekhttp://techdroid.kbeanie.comhttp://www.kbeanie.com
On Tue, Jan 18, 2011 at 2:54 PM, appel<johan.appelg...@gmail.com> wrote:
So if I check with file.exists() and before I can call open some other
process deletes the file I should always crash? That doesn't sound very user
friendly to me.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com<android-developers%2bunsubscr...@googlegroups.com>
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--
Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en