File.DeleteOnExit Method

Definition

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates.

[Android.Runtime.Register("deleteOnExit", "()V", "GetDeleteOnExitHandler")]
public virtual void DeleteOnExit ();
[<Android.Runtime.Register("deleteOnExit", "()V", "GetDeleteOnExitHandler")>]
abstract member DeleteOnExit : unit -> unit
override this.DeleteOnExit : unit -> unit
Attributes

Remarks

Requests that the file or directory denoted by this abstract pathname be deleted when the virtual machine terminates. Files (or directories) are deleted in the reverse order that they are registered. Invoking this method to delete a file or directory that is already registered for deletion has no effect. Deletion will be attempted only for normal termination of the virtual machine, as defined by the Java Language Specification.

Once deletion has been requested, it is not possible to cancel the request. This method should therefore be used with care.

Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The java.nio.channels.FileLock FileLock facility should be used instead.

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: <ul> <li>Use a finally clause to manually invoke #delete. <li>Maintain your own set of files to delete, and process it at an appropriate point in your application's lifecycle. <li>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. </ul>

Added in 1.2.

Java documentation for java.io.File.deleteOnExit().

Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.

Applies to