September 2019
Intermediate to advanced
816 pages
18h 47m
English
Another solution for deleting a temporary folder/file relies on the File.deleteOnExit() method. By calling this method, we can register for the deletion of a folder/file. The deletion action happens when JVM shuts down:
Path customBaseDir = FileSystems.getDefault().getPath("D:/tmp");String customDirPrefix = "logs_";String customFilePrefix = "log_";String customFileSuffix = ".txt";try { Path tmpDir = Files.createTempDirectory( customBaseDir, customDirPrefix); System.out.println("Created temp folder as: " + tmpDir); Path tmpFile1 = Files.createTempFile( tmpDir, customFilePrefix, customFileSuffix); Path tmpFile2 = Files.createTempFile( tmpDir, customFilePrefix, customFileSuffix); try (DirectoryStream<Path> ...