Twitter Updates

Tuesday, 5 May 2009

Java delete non-empty folders

The Java File.delete(); method only works on empty folders to delete a folder and its contents recursivly do some thing like:


   public static void recDelete(File folder) {
if(folder.exists()) {
if(folder.isDirectory()) {
File[] files = folder.listFiles();
for(int i=0; i < files.length; i++) {
File curFile = files[i];
// call to delete the current file/folder
recDelete(curFile);
}
}
//If a folder Deleted all contents
//If a File Jump straight here
folder.delete();
}
}

No comments: