Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.deleteOnExit - Unix trick from comments

Tags:

android

Here's source comment for this method:

Note that on Android, the application lifecycle does not include VM termination, so calling this method will not ensure that files are deleted. Instead, you should use the most appropriate out of:

 * Use a {@code finally} clause to manually invoke {@link #delete}.
 * Maintain your own set of files to delete, and process it at an appropriate point
    in your application's lifecycle.
 * Use the Unix trick of deleting the file as soon as all readers and writers have
   opened it. No new readers/writers will be able to access the file, but all existing
   ones will still have access until the last one closes the file.

Can anyone explain to me what is the "Unix trick" mentioned in it and how to use it?

like image 836
Lingviston Avatar asked Mar 24 '14 10:03

Lingviston


1 Answers

This answer has a good explanation: https://stackoverflow.com/a/5219960/200508. Basically, it means that "deleting" a file on a Unix system doesn't immediately erase it from disk; instead, it just deletes the reference to that file from the directory it's in. The file isn't actually erased until all processes which are using it terminate. Thus, you can open a temporary file and immediately delete it, and then whenever the program terminates it will be automatically erased.

like image 88
Cassidy Laidlaw Avatar answered Oct 20 '22 17:10

Cassidy Laidlaw