September 2019
Intermediate to advanced
816 pages
18h 47m
English
Deleting a temporary folder/file is a task that can be accomplished by the operating system or specialized tools. However, sometimes, we need to control this programmatically and delete a folder/file based on different design considerations.
A solution to this problem relies on the shutdown-hook mechanism, which can be implemented via the Runtime.getRuntime().addShutdownHook() method. This mechanism is useful whenever we need to complete certain tasks (for example, cleanup tasks) right before the JVM shuts down. It is implemented as a Java thread whose run() method is invoked when the shutdown-hook is executed by JVM at shut down. This is shown in the following code:
Path customBaseDir = ...
Read now
Unlock full access