KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > pmd > cpd > FileFinder


1 /**
2  * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
3  */

4 package net.sourceforge.pmd.cpd;
5
6 import java.io.File JavaDoc;
7 import java.io.FilenameFilter JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.List JavaDoc;
10
11 public class FileFinder {
12
13     private FilenameFilter JavaDoc filter;
14     private static final String JavaDoc FILE_SEP = System.getProperty("file.separator");
15
16     public List JavaDoc findFilesFrom(String JavaDoc dir, FilenameFilter JavaDoc filter, boolean recurse) {
17         this.filter = filter;
18         List JavaDoc files = new ArrayList JavaDoc();
19         scanDirectory(new File JavaDoc(dir), files, recurse);
20         return files;
21     }
22
23     /**
24      * Implements a tail recursive file scanner
25      */

26     private void scanDirectory(File JavaDoc dir, List JavaDoc list, boolean recurse) {
27         String JavaDoc[] candidates = dir.list(filter);
28         if (candidates == null) {
29             return;
30         }
31         for (int i = 0; i < candidates.length; i++) {
32             File JavaDoc tmp = new File JavaDoc(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 JavaDoc(dir + FILE_SEP + candidates[i]));
39             }
40         }
41     }
42 }
43
Popular Tags