View Javadoc

    1   package net.sourceforge.pmd.util;
    2
    3   import java.io.File;
    4   import java.io.FilenameFilter;
    5   import java.util.ArrayList;
    6   import java.util.List;
    7
    8   /**
    9    * A utility class for finding files within a directory.
    10   */
    11  public class FileFinder {
    12
    13      private FilenameFilter filter;
    14      private static final String FILE_SEP = System.getProperty("file.separator");
    15
    16      public List<File> findFilesFrom(String dir, FilenameFilter filter, boolean recurse) {
    17          this.filter = filter;
    18          List<File> files = new ArrayList<File>();
    19          scanDirectory(new File(dir), files, recurse);
    20          return files;
    21      }
    22
    23      /**
    24       * Implements a tail recursive file scanner
    25       */
    26      private void scanDirectory(File dir, List<File> list, boolean recurse) {
    27          String[] candidates = dir.list(filter);
    28          if (candidates == null) {
    29              return;
    30          }
    31          for (int i = 0; i < candidates.length; i++) {
    32              File tmp = new File(dir + FILE_SEP + candidates[i]);
    33              if (tmp.isDirectory()) {
    34                  if (recurse) {
    35                      scanDirectory(tmp, list, true);
    36                  }
    37              } else {
    38                  list.add(new File(dir + FILE_SEP + candidates[i]));
    39              }
    40          }
    41      }
    42  }