Recursive directory deletion in Java
Description
The following code shows how to recursive directory deletion.
Example
/*w w w .java 2 s.co m*/
import java.io.File;
public class Main {
public static boolean delete(String filePath, boolean recursive) {
File file = new File(filePath);
if (!file.exists()) {
return true;
}
if (!recursive || !file.isDirectory())
return file.delete();
String[] list = file.list();
for (int i = 0; i < list.length; i++) {
if (!delete(filePath + File.separator + list[i], true))
return false;
}
return file.delete();
}
}