< prev index next >

src/java.base/share/classes/java/io/File.java

Print this page

        

*** 1056,1071 **** /** * 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. * 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> * 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} --- 1056,1076 ---- /** * 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 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. * ! * <p> ! * 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. * * <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,1083 **** --- 1079,1089 ---- * @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,1099 **** --- 1096,1139 ---- } DeleteOnExitHook.add(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 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. + * + * <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 + * + * @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. * * <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 >