Deleting files created with FileOutputStream
Asked Answered
P

5

13

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.

Perimorph answered 20/7, 2010 at 14:53 Comment(0)
C
13

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.

Chiaki answered 21/7, 2010 at 4:23 Comment(2)
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.Perimorph
Yeah, it's really fastest way to delete file created with FileOutputStreamGoodall
L
8

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.

Larousse answered 20/7, 2010 at 14:54 Comment(9)
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."Poco
R. Bemrose: A decent OS periodically cleanups the temp directory.Larousse
@BalusC: It's a shame Windows isn't a decent OS. ;) And I can't speak for Android on how it would handle that.Poco
Agree with R. Bemrose's deleteOnExit() method. It's been very useful in the past.Antisepticize
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.Perimorph
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.Perimorph
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).Larousse
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.Perimorph
What would be a better approach for this kind of behavior? I thought simple flat files would be enough.Perimorph
L
1

you need to close the file, before deleting it. use below code.

        FileOutputStream fos = openFileOutput("MY_TEMP.TXT",Mode);
        File f = new File(System.getProperty("user.dir"),"MY_TEMP.TXT");
        fos.close();
        File f = new File("MY_TEMP.TXT");
        f.delete();
Livvyy answered 27/7, 2013 at 17:56 Comment(0)
G
0

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

Graeme answered 20/7, 2010 at 14:57 Comment(0)
F
0

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.

Flossi answered 20/7, 2010 at 15:0 Comment(1)
I'm not saying it would. Just another way to delete files :)Flossi

© 2022 - 2024 — McMap. All rights reserved.