File.deleteOnExit - Unix trick from comments

Here is the original comment for this method:

Please note that on Android, the application life cycle does not include VM termination, so calling this method does not guarantee that the files will be deleted. Instead, you should use the most suitable 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 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 someone explain to me what the “Unix trick” mentioned in it is and how to use it?

+8
android
source share
1 answer

This answer has a good explanation: stack overflow . This basically means that “deleting” a file on a Unix system does not immediately delete it from disk; instead, it simply removes the link to this file from the directory in which it is located. The file is not actually deleted until all processes that use it have stopped. Thus, you can open a temporary file and delete it immediately, and then whenever the program terminates, it will be automatically deleted.

0
source share

All Articles