--- old/src/java.base/share/classes/java/io/DeleteOnExitHook.java 2019-07-09 08:04:12.000000000 -0700 +++ new/src/java.base/share/classes/java/io/DeleteOnExitHook.java 2019-07-09 08:04:12.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 08:04:13.000000000 -0700 +++ new/src/java.base/share/classes/java/io/File.java 2019-07-09 08:04:12.000000000 -0700 @@ -164,6 +164,11 @@ private final String path; /** + * Flag indicating whether this file path should be deleted on VM exit. + */ + private boolean deleteOnExit; + + /** * Enum type that indicates the status of a file path. */ private static enum PathStatus { INVALID, CHECKED }; @@ -1021,7 +1026,11 @@ if (isInvalid()) { throw new IOException("Invalid file path"); } - return fs.createFileExclusively(path); + boolean result = fs.createFileExclusively(path); + if (deleteOnExit) { + deleteOnExit(); + } + return result; } /** @@ -1050,6 +1059,7 @@ if (isInvalid()) { return false; } + DeleteOnExitHook.remove(path); return fs.delete(this); } @@ -1089,6 +1099,7 @@ return; } DeleteOnExitHook.add(path); + deleteOnExit = true; } /** @@ -1325,7 +1336,11 @@ if (isInvalid()) { return false; } - return fs.createDirectory(this); + boolean result = fs.createDirectory(this); + if (deleteOnExit) { + deleteOnExit(); + } + return result; } /** @@ -1363,8 +1378,12 @@ } File parent = canonFile.getParentFile(); - return (parent != null && (parent.mkdirs() || parent.exists()) && - canonFile.mkdir()); + boolean result = (parent != null && + (parent.mkdirs() || parent.exists()) && canonFile.mkdir()); + if (deleteOnExit) { + deleteOnExit(); + } + return result; } /**