Table of Contents
In this post, we will see how to delete Directory/Folder which is non empty. You can use java.io.File ‘s delete empy folder but you can not delete it if it is non empty.
There are multiple ways to do it.
When I ran above program, I got following output
There are multiple ways to do it.
- Using java recursion
- Using Apache common IO
Using java recursion:
It is very straight forward to do it. It will iterate through all files in the folder. If it is a file then we can directly delete it but if It finds folder inside it then we again call same method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
// Delete using recursion public static void delete(File file) throws IOException{ if(file.isDirectory()){ //We can directly delete if we found empty directory if(file.list().length==0){ file.delete(); System.out.println("Deleting folder : " + file.getAbsolutePath()); }else{ //list all the files in directory File files[] = file.listFiles(); for (File temp : files) { //recursive delete delete(temp); } // Check directory again, if we find it empty, delete it if(file.list().length==0){ file.delete(); System.out.println("Deleting folder : " + file.getAbsolutePath()); } } }else{ //if file, then we can directly delete it file.delete(); System.out.println("Deleting file : " + file.getAbsolutePath()); } } |
Using Apache common IO:
Delete non empty folder is very straight forward. You just need to call FileUtils.deleteDirectory() method.
1 2 3 4 5 6 7 8 9 10 11 12 |
// Delete using Apache common IO public static void deleteUsingApacheIO(File f) { try { FileUtils.deleteDirectory(f); } catch (IOException e) { e.printStackTrace(); } } |
Java Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
package org.arpit.java2blog; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; public class DeleteNonEmptyDirectoryMain { public static void main(String[] args) { System.out.println("Deleting using recursion"); System.out.println("---------------"); File folder= new File("/Users/Arpit/Desktop/Blog/temp1"); // Using recusrion try { delete(folder); } catch (IOException e) { e.printStackTrace(); } System.out.println("---------------"); System.out.println("Deleting using Apache IO"); File folder2= new File("/Users/Arpit/Desktop/Blog/temp2"); deleteUsingApacheIO(folder2); System.out.println("Deleting folder : " + folder2.getAbsolutePath()); } // Delete using recursion public static void delete(File file) throws IOException{ if(file.isDirectory()){ //We can directly delete if we found empty directory if(file.list().length==0){ file.delete(); System.out.println("Deleting folder : " + file.getAbsolutePath()); }else{ //list all the files in directory File files[] = file.listFiles(); for (File temp : files) { //recursive delete delete(temp); } // Check directory again, if we find it empty, delete it if(file.list().length==0){ file.delete(); System.out.println("Deleting folder : " + file.getAbsolutePath()); } } }else{ //if file, then we can directly delete it file.delete(); System.out.println("Deleting file : " + file.getAbsolutePath()); } } // Delete using Apache common IO public static void deleteUsingApacheIO(File f) { try { FileUtils.deleteDirectory(f); } catch (IOException e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Deleting using recursion --------------- Deleting file : /Users/Arpit/Desktop/Blog/temp1/.DS_Store Deleting file : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/.DS_Store Deleting file : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/verticalSum.jpg.png Deleting file : /Users/Arpit/Desktop/Blog/temp1/BinaryTree/VerticalSumOrder.png Deleting folder : /Users/Arpit/Desktop/Blog/temp1/BinaryTree Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/.DS_Store Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyJsonCountry.png Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyJsonCountryID.png Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyMavenBuild.png Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyMavenCleanInstall.png Deleting file : /Users/Arpit/Desktop/Blog/temp1/jersey json/jerseyProjectStructure.png Deleting folder : /Users/Arpit/Desktop/Blog/temp1/jersey json Deleting folder : /Users/Arpit/Desktop/Blog/temp1 --------------- Deleting using Apache IO Deleting folder : /Users/Arpit/Desktop/Blog/temp2 |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.