--- old/src/java.base/share/classes/java/io/File.java 2019-07-11 09:34:41.860301470 +0200 +++ new/src/java.base/share/classes/java/io/File.java 2019-07-11 09:34:41.739301887 +0200 @@ -1058,12 +1058,18 @@ * 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. * - *

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 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. * *

* Note: this method should not be used for file-locking, as @@ -1076,6 +1082,7 @@ * java.lang.SecurityManager#checkDelete} method denies * delete access to the file * + * @see #cancelDeleteOnExit * @see #delete * * @since 1.2 @@ -1088,7 +1095,45 @@ 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. + * + *

+ * 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); } /**