Using the FileUtils.deleteDirectory()
method can help to simplify the process of deleting directory and everything below it recursively.
package org.kodejava.commons.io;
import org.apache.commons.io.FileUtils;
import java.io.File;
import java.io.IOException;
public class DeleteDirectory {
public static void main(String[] args) {
try {
File directory = new File("F:/Temp");
// Deletes a directory recursively. When deletion process is fail an
// IOException is thrown and that's why we catch the exception.
FileUtils.deleteDirectory(directory);
} catch (IOException e) {
e.printStackTrace();
}
}
}
Maven Dependencies
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
Latest posts by Wayan (see all)
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023
- How do I calculate days between two dates excluding weekends and holidays? - January 12, 2023
- How do I discover the quarter of a given date? - January 11, 2023