Java examples for File Path IO:File Permission
Deleting a Temporary Directory with Shutdown-Hook
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 {/* w w w . j a va 2 s . co m*/ // create a tmp directory in the base dir 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.println(e); } System.out.println("Shutdown-hook completed..."); } }); Thread.sleep(10000); } catch (IOException | InterruptedException e) { System.err.println(e); } } }