Source Code : Delete all files under this file and including this file

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Delete all files under this file and including this file

 
import java.io.File;
import java.io.IOException;

public class Utils {
  /**
   * delete all files under this file and including this file
   * 
   * @param f
   * @throws IOException
   */
  public static void deleteAll(File f) throws IOException {
    recurse(f, new RecurseAction() {
      public void doFile(File file) throws IOException {
        file.delete();
        if (file.exists()) {
          throw new IOException("Failed to delete  " + file.getPath());
        }
      }

      public void doBeforeFile(File f) {
      }

      public void doAfterFile(File f) {
      }
    });
  }

  public static void recurse(File f, RecurseAction action) throws IOException {
    action.doBeforeFile(f);
    if (f.isDirectory()) {
      File[] files = f.listFiles();
      if (files != null) {
        for (int i = 0; i < files.length; i++) {
          if (files[i].isDirectory()) {
            recurse(files[i], action);
          } else {
            action.doFile(files[i]);
          }
        }
      }
    }
    action.doFile(f);
  }
}

interface RecurseAction {

  /**
   * @param file
   * @throws IOException
   */
  void doFile(File file) throws IOException;

  /**
   * @param f
   */
  void doBeforeFile(File f);

  /**
   * @param f
   */
  void doAfterFile(File f);

}

   
  

Thank with us