< prev index next >
src/java.base/share/classes/java/io/File.java
Print this page
@@ -1056,16 +1056,22 @@
/**
* 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.
+ * directory that is already registered for deletion causes its
+ * deletion registration reference count to be incremented and the
+ * file registration order to be updated.
* 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>
+ * An invocation of this method may be cancelled by invoking
+ * {@link #cancelDeleteOnExit()}. There must be exactly as many
+ * cancellation as deletion requests however to unregister the file
+ * or directory completely from eventual deletion. Cancellation which
+ * doesn't unregister the file does not affect the order of deletion.
*
* <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 +1080,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() {
@@ -1086,11 +1093,49 @@
security.checkDelete(path);
}
if (isInvalid()) {
return;
}
- DeleteOnExitHook.add(path);
+ DeleteOnExitHook.deleteOnExit(path);
+ }
+
+ /**
+ * Cancels a 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 throws exception. This method will cause the deletion registration
+ * reference count of a registered file or directory to be decremented but
+ * will not unregister it unless that count reaches zero neither will it
+ * change the order of file registration.
+ *
+ * <p>
+ * 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.
+ *
+ * @throws SecurityException
+ * If a security manager exists and its {@link
+ * java.lang.SecurityManager#checkDelete} method denies
+ * delete access to the file
+ * @throws IllegalStateException
+ * If this method is called on the same file unbalanced
+ * with the {@link #deleteOnExit()}
+ *
+ * @see #deleteOnExit
+ *
+ * @since 14
+ */
+ public void cancelDeleteOnExit() {
+ SecurityManager security = System.getSecurityManager();
+ if (security != null) {
+ security.checkDelete(path);
+ }
+ if (isInvalid()) {
+ return;
+ }
+ DeleteOnExitHook.cancelDeleteOnExit(path);
}
/**
* Returns an array of strings naming the files and directories in the
* directory denoted by this abstract pathname.
< prev index next >