delete Directory with file filter - Android java.io

Android examples for java.io:Directory

Description

delete Directory with file filter

Demo Code

import java.io.File;
import java.io.FileFilter;
import java.io.FilenameFilter;

public class Main {

  /**/*from ww w  . j  av a  2 s.c  o m*/
   * Clean a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   */
  public static void cleanDir(final File dir) {
    deleteDir(dir, false);
  }

  /**
   * Clean a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void cleanDir(final File dir, final FilenameFilter filter) {
    deleteDir(dir, false, filter);
  }

  /**
   * Clean a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void cleanDir(final File dir, final FileFilter filter) {
    deleteDir(dir, false, filter);
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param removeDir
   *          true to remove the {@code dir}.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void deleteDir(final File dir, final boolean removeDir, final FilenameFilter filter) {
    if (dir != null && dir.isDirectory()) {
      final File[] files = dir.listFiles(filter);
      if (files != null) {
        for (final File file : files) {
          if (file.isDirectory()) {
            deleteDir(file, removeDir, filter);
          } else {
            file.delete();
          }
        }
      }
      if (removeDir) {
        dir.delete();
      }
    }
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void deleteDir(final File dir, final FileFilter filter) {
    deleteDir(dir, true, filter);
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param removeDir
   *          true to remove the {@code dir}.
   */
  public static void deleteDir(final File dir, final boolean removeDir) {
    if (dir != null && dir.isDirectory()) {
      final File[] files = dir.listFiles();
      for (final File file : files) {
        if (file.isDirectory()) {
          deleteDir(file, removeDir);
        } else {
          file.delete();
        }
      }
      if (removeDir) {
        dir.delete();
      }
    }
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param removeDir
   *          true to remove the {@code dir}.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void deleteDir(final File dir, final boolean removeDir, final FileFilter filter) {
    if (dir != null && dir.isDirectory()) {
      final File[] files = dir.listFiles(filter);
      if (files != null) {
        for (final File file : files) {
          if (file.isDirectory()) {
            deleteDir(file, removeDir, filter);
          } else {
            file.delete();
          }
        }
      }
      if (removeDir) {
        dir.delete();
      }
    }
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   * @param filter
   *          the filter to determine which file or directory to delete.
   */
  public static void deleteDir(final File dir, final FilenameFilter filter) {
    deleteDir(dir, true, filter);
  }

  /**
   * Delete a specified directory.
   * 
   * @param dir
   *          the directory to clean.
   */
  public static void deleteDir(final File dir) {
    deleteDir(dir, true);
  }

}

Related Tutorials