Using the FileUtils.deleteDirectory()
method can help to simplify the process of deleting directory and everything below it recursively.
package org.kodejava.example.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("/home/foobar/Temp/Data");
// 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
<!-- http://repo1.maven.org/maven2/commons-io/commons-io/2.6/commons-io-2.6.jar -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
Latest posts by Wayan (see all)
- How do I install Calibri font in Ubuntu? - January 24, 2021
- How do I create a generic class in Java? - January 1, 2021
- How do I convert java.util.TimeZone to java.time.ZoneId? - April 25, 2020