brian brian - 5 months ago 29
Java Question

How to delete all files and folders in one folder on Android

I use this code to delete all files:

File root = new File("root path");
File[] Files = root.listFiles();
if(Files != null) {
int j;
for(j = 0; j < Files.length; j++) {
System.out.println(Files[j].getAbsolutePath());
System.out.println(Files[j].delete());
}
}


It will delete false where
Files[j]
is a folder.

I want to delete folder and all its sub files.

How can I modify this?

Answer

Simplest way would be to use FileUtils.deleteDirectory from the Apache Commons IO library.

File dir = new File("root path");
FileUtils.deleteDirectory(dir);