< prev index next >
src/java.base/share/classes/java/io/File.java
Print this page
@@ -1060,12 +1060,13 @@
* 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.
*
- * <p> Once deletion has been requested, it is not possible to cancel the
- * request. This method should therefore be used with care.
+ * <p>
+ * Once deletion has been requested, it may be cancelled by invoking
+ * {@link #cancelDeleteOnExit()}.
*
* <P>
* Note: this method should <i>not</i> be used for file-locking, as
* the resulting protocol cannot be made to work reliably. The
* {@link java.nio.channels.FileLock FileLock}
@@ -1074,10 +1075,11 @@
* @throws SecurityException
* If a security manager exists and its {@link
* java.lang.SecurityManager#checkDelete} method denies
* delete access to the file
*
+ * @see #cancelDeleteOnExit
* @see #delete
*
* @since 1.2
*/
public void deleteOnExit() {
@@ -1090,10 +1092,30 @@
}
DeleteOnExitHook.add(path);
}
/**
+ * Cancels any request that the file or directory denoted by this
+ * abstract pathname be deleted when the virtual machine terminates.
+ * Invoking this method for a file or directory that is not already
+ * registered for deletion has no effect. If a file or directory is
+ * registered for deletion but is explicitly deleted before normal
+ * termination of the virtual machine, then it is recommended to call
+ * this method to free resources used to track the file for deletion.
+ *
+ * @see #deleteOnExit
+ *
+ * @since 14
+ */
+ public void cancelDeleteOnExit() {
+ if (isInvalid()) {
+ return;
+ }
+ DeleteOnExitHook.remove(path);
+ }
+
+ /**
* Returns an array of strings naming the files and directories in the
* directory denoted by this abstract pathname.
*
* <p> If this abstract pathname does not denote a directory, then this
* method returns {@code null}. Otherwise an array of strings is
< prev index next >