This article will show you how to use java.io
, java.new.io
and org.apache.commons.io.FileUtils
to delete directory recursively in java. The example java code is self explanation, please see the comments for more detail.
1. Use java.io.File.delete()
Example code for delete directory or file recursively.
/* Delete directory use Java.io. */ public void delDir(String dirName) { try { File dirFile = new File(dirName); if(dirFile.exists()) { if(dirFile.isDirectory()) { /* Get sub directory or file array. */ File subFileArr[] = dirFile.listFiles(); int subFileLen = subFileArr.length; /* Delete empty directory. */ if(subFileLen==0) { boolean delResult = dirFile.delete(); if(delResult) { System.out.println("Remove " + dirName + " success."); }else { System.out.println("Remove " + dirName + " fail."); } }else { /* If current folder is not empty, then delete all it's sub folder. */ for(File subFile : subFileArr) { this.delDir(subFile.getCanonicalPath()); } /* After delete all it's sub item, delete current folder. */ if(dirFile.listFiles().length==0) { boolean delResult = dirFile.delete(); if(delResult) { System.out.println("Remove " + dirName + " success."); }else { System.out.println("Remove " + dirName + " fail."); } } } }else { /* If this is a file, then delte it directly. */ boolean delResult = dirFile.delete(); if(delResult) { System.out.println("Remove " + dirName + " success."); }else { System.out.println("Remove " + dirName + " fail."); } } }else { System.out.println(dirName + " dose not exist. "); } }catch(Exception ex) { ex.printStackTrace(); }finally { System.out.println("Remove " + dirName + " complete. "); } }
2. Use java.new.io
- Create a File visitor object by extend
java.nio.file.SimpleFileVisitor
. - Override
postVisitDirectory(Path dir, IOException arg1)
andvisitFile(Path file, BasicFileAttributes arg1)
. JavaNewIOFileVisitor
public class JavaNewIOFileVisitor extends SimpleFileVisitor { @Override public FileVisitResult postVisitDirectory(Path dir, IOException arg1) throws IOException { boolean delResult = Files.deleteIfExists(dir); if(delResult) { System.out.println("Remove " + dir.getFileName().toString() + " success. "); }else { System.out.println("Remove " + dir.getFileName().toString() + " fail. "); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFile(Path file, BasicFileAttributes arg1) throws IOException { boolean delResult = Files.deleteIfExists(file); if(delResult) { System.out.println("Remove " + file.getFileName().toString() + " success. "); }else { System.out.println("Remove " + file.getFileName().toString() + " fail. "); } return FileVisitResult.CONTINUE; } }
- Use the customize file visitor to go through the path tree and delete sub directory recursively. You can click here to see java new io document.
/* Delete directory use Java.new.io. */ public void delDirUseJavaNewIO(String dirName) { try { File dirFile = new File(dirName); if(dirFile.exists()) { /* Create a path object point to the deleted directory. */ Path dirPath = Paths.get(dirName); /* Create the custom file visitor object. */ JavaNewIOFileVisitor fileVisitor = new JavaNewIOFileVisitor(); /* Go through the path tree and invoke the related file visitor's method. */ Files.walkFileTree(dirPath, fileVisitor); }else { System.out.println(dirName + " dose not exist. "); } }catch(Exception ex) { ex.printStackTrace(); }finally { System.out.println("Remove " + dirName + " complete. "); } }
3. Use org.apache.commons.io.FileUtils
This is the easiest way to delete a directory recursively. You just need only one line java code. But before you can do that, you should first add Apache commons io jar file into your java project. You can read article Copy Directory Examples ‘s use Apache commons io section to learn how to use it.
/* Delete directory use Apache.commons.io. */ public void delDirUseApacheCommonsIO(String dirName) { try { File dirFile = new File(dirName); if(dirFile.exists()) { /* Only need below one line code. */ boolean delResult = FileUtils.deleteQuietly(dirFile); if(delResult) { System.out.println("Remove " + dirName + " success. "); }else { System.out.println("Remove " + dirName + " fail. "); } } }catch(Exception ex) { ex.printStackTrace(); }finally { System.out.println("Remove " + dirName + " complete. "); } }
4. Java main method
public static void main(String[] args) { DeleteDirectoryExample dde = new DeleteDirectoryExample(); dde.delDir("C:/WorkSpace/dev2qa_copy"); dde.delDirUseJavaNewIO("C:/WorkSpace/dev2qa_copy"); dde.delDirUseApacheCommonsIO("C:/WorkSpace/dev2qa_copy"); }
[download id=”2230″]