Search code examples
javafilefile-iodirectorytemporary-files

How to create a temporary directory which will be automatically deleted whem JVM terminates?


JDK 7 introduces an API called Files.createTempDirectory, but the directory created by the API is not automatically deleted when the JVM terminates.

And unfortunately File.deleteOnExit does not work with a non-empty directory.

Is there a way to create a directory which is automatically deleted when JVM terminates?


Solution

  • register custom shutdown hook with something like deleteDirectory from Commons IO