Delete a directory


/ Published in: Java
Save to your folder(s)

A "quick and dirty" way to delete a directory. For more cleaner implementation, see http://www.google.com/codesearch/p?hl=en#SS2by_AKaLs/src/org/apache/commons/io/FileUtils.java&q=cleanDirectory&l=972


Copy this code and paste it in your HTML
  1. public static boolean deleteDir(File dir) {
  2. if (dir.isDirectory()) {
  3. String[] children = dir.list();
  4. for (int i=0; i<children.length; i++) {
  5. boolean success = deleteDir(new File(dir, children[i]));
  6. if (!success) {
  7. return false;
  8. }
  9. }
  10. }
  11.  
  12. // The directory is now empty so delete it
  13. return dir.delete();
  14. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.