|
1 | 1 | package processing.app.helpers;
|
2 | 2 |
|
3 | 3 | import java.io.*;
|
| 4 | +import java.util.ArrayList; |
4 | 5 | import java.util.Arrays;
|
5 | 6 | import java.util.List;
|
6 | 7 | import java.util.Random;
|
@@ -188,4 +189,71 @@ public static String readFileToString(File file) throws IOException {
|
188 | 189 | }
|
189 | 190 | }
|
190 | 191 | }
|
| 192 | + |
| 193 | + /** |
| 194 | + * Returns true if the given file has any of the given extensions. |
| 195 | + * @param file |
| 196 | + * File whose name to look at |
| 197 | + * @param extensions |
| 198 | + * Extensions to consider (just the extension, without the |
| 199 | + * dot). Should all be lowercase, case insensitive matching |
| 200 | + * is used. |
| 201 | + */ |
| 202 | + public static boolean hasExtension(File file, String... extensions) { |
| 203 | + return hasExtension(file, Arrays.asList(extensions)); |
| 204 | + } |
| 205 | + |
| 206 | + public static boolean hasExtension(File file, List<String> extensions) { |
| 207 | + String pieces[] = file.getName().split("\\."); |
| 208 | + if (pieces.length < 2) |
| 209 | + return false; |
| 210 | + |
| 211 | + String extension = pieces[pieces.length - 1]; |
| 212 | + |
| 213 | + return extensions.contains(extension.toLowerCase()); |
| 214 | + |
| 215 | + } |
| 216 | + |
| 217 | + /** |
| 218 | + * Recursively find all files in a folder with the specified |
| 219 | + * extension. Excludes hidden files and folders and |
| 220 | + * source control folders. |
| 221 | + * |
| 222 | + * @param folder |
| 223 | + * Folder to look into |
| 224 | + * @param recursive |
| 225 | + * <b>true</b> will recursively find all files in sub-folders |
| 226 | + * @param extensions |
| 227 | + * A list of file extensions to search (just the extension, |
| 228 | + * without the dot). Should all be lowercase, case |
| 229 | + * insensitive matching is used. If no extensions are |
| 230 | + * passed, all files are returned. |
| 231 | + * @return |
| 232 | + */ |
| 233 | + public static List<File> listFiles(File folder, boolean recursive, |
| 234 | + String... extensions) { |
| 235 | + return listFiles(folder, recursive, Arrays.asList(extensions)); |
| 236 | + } |
| 237 | + |
| 238 | + public static List<File> listFiles(File folder, boolean recursive, |
| 239 | + List<String> extensions) { |
| 240 | + List<File> result = new ArrayList<File>(); |
| 241 | + |
| 242 | + for (File file : folder.listFiles()) { |
| 243 | + if (isSCCSOrHiddenFile(file)) |
| 244 | + continue; |
| 245 | + |
| 246 | + if (file.isDirectory()) { |
| 247 | + if (recursive) |
| 248 | + result.addAll(listFiles(file, true, extensions)); |
| 249 | + continue; |
| 250 | + } |
| 251 | + |
| 252 | + if (extensions.isEmpty() || hasExtension(file, extensions)) |
| 253 | + result.add(file); |
| 254 | + } |
| 255 | + return result; |
| 256 | + } |
| 257 | + |
| 258 | + |
191 | 259 | }
|
0 commit comments