Skip to content

Commit 60c24ef

Browse files
committed
FileUtils: added function to recursively find files with given extension
1 parent 66974e7 commit 60c24ef

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

app/src/processing/app/helpers/FileUtils.java

+27
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package processing.app.helpers;
22

33
import java.io.*;
4+
import java.util.ArrayList;
45
import java.util.Arrays;
56
import java.util.List;
67
import java.util.Random;
@@ -188,4 +189,30 @@ public static String readFileToString(File file) throws IOException {
188189
}
189190
}
190191
}
192+
193+
/**
194+
* Recursively find all files in a folder with the specified extension
195+
*
196+
* @param folder
197+
* @param extensions
198+
* @return
199+
*/
200+
public static List<File> listAllFilesWithExtension(File folder, String... extensions) {
201+
List<File> result = new ArrayList<File>();
202+
for (File file : folder.listFiles()) {
203+
if (file.isDirectory()) {
204+
result.addAll(listAllFilesWithExtension(file, extensions));
205+
continue;
206+
}
207+
208+
for (String ext : extensions) {
209+
if (file.getName().endsWith(ext)) {
210+
result.add(file);
211+
break;
212+
}
213+
}
214+
}
215+
return result;
216+
}
217+
191218
}

0 commit comments

Comments
 (0)