--- old/src/java.base/share/classes/java/io/File.java 2019-07-10 17:11:43.000000000 -0700 +++ new/src/java.base/share/classes/java/io/File.java 2019-07-10 17:11:43.000000000 -0700 @@ -1058,12 +1058,17 @@ * 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. * 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. + *
+ * An invocation of this method may be cancelled by invoking + * {@link #cancelDeleteOnExit()}. There must be at least as many + * cancellation as deletion requests however to unregister the file + * or directory completely from eventual deletion. Cancellation does + * not affect the order of deletion. * *
* Note: this method should not be used for file-locking, as @@ -1076,6 +1081,7 @@ * java.lang.SecurityManager#checkDelete} method denies * delete access to the file * + * @see #cancelDeleteOnExit * @see #delete * * @since 1.2 @@ -1092,6 +1098,40 @@ } /** + * 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 has no effect. 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. + * + *
+ * 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 + * + * @see #deleteOnExit + * + * @since 14 + */ + public void cancelDeleteOnExit() { + SecurityManager security = System.getSecurityManager(); + if (security != null) { + security.checkDelete(path); + } + if (isInvalid()) { + return; + } + DeleteOnExitHook.remove(path); + } + + /** * Returns an array of strings naming the files and directories in the * directory denoted by this abstract pathname. *