views:

153

answers:

4

Hi, I'm developing for the Android platform. My app creates a temp file with a simple call to:

FileOutputStream fos = openFileOutput("MY_TEMP.TXT",Mode);

It works fine because I can write to it and read it normally.

The problem is that when I exit from the app I want to delete this file. I used:

File f = new File(System.getProperty("user.dir"),"MY_TEMP.TXT");
f.delete()

But it always returns false and the file is not deleted. I have tried

File f = new File("MY_TEMP.TXT");
f.delete();

And it does not work either. Please advise!

+4  A: 

You can't delete an opened file. You need to close the stream before delete.

fos.close();
f.delete();

That said, I would rather use File#createTempFile() to let the underlying platform do the automatic cleanup work and to avoid potential portability trouble caused by using relative paths in File.

BalusC
From `createTempFile`'s docs: "This method provides only part of a temporary-file facility. To arrange for a file created by this method to be deleted automatically, use the `deleteOnExit()` method."
R. Bemrose
R. Bemrose: A decent OS periodically cleanups the temp directory.
BalusC
@BalusC: It's a shame Windows isn't a decent OS. ;) And I can't speak for Android on how it would handle that.
R. Bemrose
Agree with R. Bemrose's `deleteOnExit()` method. It's been very useful in the past.
Justian Meyer
Thank you all for your responses. I closed the FileOutputStream object as soon as I finish working with it and before trying to delete it. 1 thing that worries me with temp files is that I need to keep the information of the file at hand until the user decides to finish the activity process. This is tricky because I don't want to lose the file contents when a call is received or if the user presses the home or back buttons. If the activity is stopped i need to preserve the data because it is a sequential process. File must be deleted UNTIL the user clicks the finish button.
ctellez69
That could be on the same session or until tomorrow. The user can be pausing the process.Because these are small pieces of information there's no need to use a database and all its hurdle. It's simple enough to use a flat file.
ctellez69
Ah yes, if it's not a temporary file, then you should also not create it as a temporary file :) One other thing which worries me, is the problem solved? Just wondering since the answer isn't marked accepted and your second statement is a bit ambiguous (it can also be interpreted as if you *already* took care of closing).
BalusC
Oh, sorry for the confusion! I should have not called temp file. No the problem is not solved yet. I tried to explain the details of the process in the 2nd posting. Actually what it is happening is that I close the activity by clicking on the finish button (calling the delete for the file) and then finish the Activity. When I load the Activity again it brings back the contents of the file.
ctellez69
What would be a better approach for this kind of behavior? I thought simple flat files would be enough.
ctellez69
A: 

Double-check, if the Stream is closed before you attempt to delete the file.

Andreas_D
A: 

You have some solid answers already, but I just want to mention File.deleteOnExit() which schedules a file for deletion when the VM exits.

--edit--

You still should close any streams connected to the file.

willcodejavaforfood
That won't work when you don't close the file.
BalusC
I'm not saying it would. Just another way to delete files :)
willcodejavaforfood
A: 

Hi everybody, I checked on this posting and the best way to delete a file created from FileOutputStream is a simple call from the Context method deleteFile(TEMP_FILE) as simple as that.

That's correct! It is extremely simple!just calling deleteFile(TEMP_FILE);It goes directly to the folder assigned to the profile and it is portable according to the documentation. It works for my need.Wow I was totally on the wrong track! Anyway, thank you all you guys for pointing me in the correct direction.
ctellez69