当JVM关闭时,deleteOnExit()方法将删除传递的文件或目录。
import java.io.File; import java.io.IOException; import java.nio.file.FileSystems;/** 来自 N o w J a v a . c o m - 时 代 Java**/ import java.nio.file.Files; import java.nio.file.Path; public class Main { public static void main(String[] args) { Path basedir = FileSystems.getDefault().getPath("C:/folder1/tmp/"); String tmp_dir_prefix = "test_"; try { // create a tmp directory in the base dir Path tmp_dir = Files.createTempDirectory(basedir, tmp_dir_prefix); File asFile = tmp_dir.toFile(); asFile.deleteOnExit(); Thread.sl