1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.util;
39
40 import java.io.File;
41 import java.io.FileFilter;
42 import java.util.ArrayList;
43 import java.util.Collection;
44 import java.util.LinkedList;
45 import java.util.List;
46
47 /***
48 * A class to walk through the directory structure from a given starting point
49 * and return either files or directories or both.<p>
50 *
51 * The following sample gets all java files.
52 * <pre>
53 * final FileFilter filter = new FileFilter() {
54 * public boolean accept( final File file ) {
55 * return file.getName().endsWith(".java");
56 * }
57 * };
58 *
59 * final DirectoryWalker directoryWalker = new DirectoryWalker(".");
60 * final Collection files = directoryWalker.getFiles(filter);
61 * </pre>
62 *
63 * @version $Revision: 1.6 $
64 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
65 */
66 public class DirectoryWalker {
67 private final File startingDirectory_;
68
69
70 /***
71 * Create an instance
72 *
73 * @param startingDirectory the directory to start in
74 */
75 public DirectoryWalker( final String startingDirectory ) {
76 if( startingDirectory == null ) {
77 throw new NullPointerException();
78 }
79 if( startingDirectory.length() == 0 ) {
80 throw new DetailedIllegalArgumentException( "startingDirectory", startingDirectory, "May not be empty" );
81 }
82 startingDirectory_ = new File( startingDirectory );
83 if( startingDirectory_.exists() == false ) {
84 throw new DetailedIllegalArgumentException( "startingDirectory", startingDirectory, "Doesn't exist" );
85 }
86 if( startingDirectory_.isDirectory() == false ) {
87 throw new DetailedIllegalArgumentException( "startingDirectory", startingDirectory, "Not a directory" );
88 }
89 }
90
91
92 /***
93 * Walk through the directory structure and return a collection containing
94 * all those files for which the filter returns true
95 *
96 * @param filter An object to determine whether or not to include this file
97 * in the returned collection
98 * @return A collection of File objects
99 */
100 public Collection getFiles( final FileFilter filter ) {
101
102 return walk( filter, true, false );
103 }
104
105
106 /***
107 * Walk through the directory structure and return a collection containing
108 * all those directories for which the filter returns true
109 *
110 * @param filter An object to determine whether or not to include this
111 * directory in the returned collection
112 * @return A collection of File objects
113 */
114 public Collection getDirectories( final FileFilter filter ) {
115 return walk( filter, false, true );
116 }
117
118
119 /***
120 * Walk through the directory structure and return a collection containing
121 * all those files and directories for which the filter returns true
122 *
123 * @param filter An object to determine whether or not to include this
124 * file/directory in the returned collection
125 * @return A collection of File objects
126 */
127 public Collection getFilesAndDirectories( final FileFilter filter ) {
128 return walk( filter, true, true );
129 }
130
131
132 private Collection walk(
133 final FileFilter filter,
134 final boolean includeFiles,
135 final boolean includeDirectories ) {
136
137 int i;
138 final Collection results = new ArrayList();
139 final List pendingDirectories = new LinkedList();
140 pendingDirectories.add( startingDirectory_ );
141
142 File dir;
143
144 if( includeDirectories && filter.accept( startingDirectory_ ) ) {
145 results.add( startingDirectory_ );
146 }
147
148 while( pendingDirectories.isEmpty() == false ) {
149 dir = (File)pendingDirectories.get( 0 );
150 pendingDirectories.remove( 0 );
151
152 final File[] files = dir.listFiles();
153 for( i = 0; i < files.length; i++ ) {
154 final File currentFile = files[i];
155 if( includeFiles && currentFile.isFile() && filter.accept( currentFile ) ) {
156 results.add( currentFile );
157 }
158 else if( currentFile.isDirectory() ) {
159 if( includeDirectories && filter.accept( currentFile ) ) {
160 results.add( currentFile );
161 }
162
163 pendingDirectories.add( currentFile );
164 }
165 }
166 }
167
168 return results;
169 }
170 }
171