使用关机挂钩删除临时目录
/*时代Java公众号 - nowjava.com 提供*/ import java.io.IOException; import java.nio.file.DirectoryStream; import java.nio.file.FileSystems; import java.nio.file.Files; import java.nio.file.Path; public class Main { public static void main(String[] args) { final Path basedir = FileSystems.getDefault() .getPath("C:/folder1/tmp/"); final String tmp_dir_prefix = "test_"; try { // create a tmp directory in the base dir /* 时 代 J a v a - nowjava.com */ Path tmp_dir = Files.createTempDirectory(basedir, tmp_dir_prefix); Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { System.out.println("Deleting the temporary folder ..."); try (DirectoryStream<Path> ds = Files.newDirectoryStream(tmp_dir)) { for (Path file : ds) { Files.delete(file); } Files.delete(tmp_dir); } catch (IOException e) { System.err.pr