--- old/src/java.base/share/classes/java/io/DeleteOnExitHook.java 2019-07-09 11:05:44.000000000 -0700 +++ new/src/java.base/share/classes/java/io/DeleteOnExitHook.java 2019-07-09 11:05:44.000000000 -0700 @@ -1,5 +1,5 @@ /* - * Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved. + * Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it @@ -64,6 +64,12 @@ files.add(file); } + static synchronized void remove(String file) { + if(files != null) { + files.remove(file); + } + } + static void runHooks() { LinkedHashSet theFiles; --- old/src/java.base/share/classes/java/io/File.java 2019-07-09 11:05:45.000000000 -0700 +++ new/src/java.base/share/classes/java/io/File.java 2019-07-09 11:05:44.000000000 -0700 @@ -1062,8 +1062,9 @@ * 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. + *

+ * Once deletion has been requested, it may be cancelled by invoking + * {@link #cancelDeleteOnExit()}. * *

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