/** * Invokes the closure for each 'child' file in this 'parent' folder/directory. * Both regular files and subfolders/subdirectories can be processed depending * on the fileType enum value. * * @param self a Path (that happens to be a folder/directory) * @param fileType if normal files or directories or both should be processed * @param closure the closure to invoke * @throws java.io.FileNotFoundException if the given directory does not exist * @throws IllegalArgumentException if the provided Path object does not represent a directory * @since 2.3.0 */ public static void eachFile(final Path self, final FileType fileType, @ClosureParams(value = SimpleType.class, options = "java.nio.file.Path") final Closure closure) throws IOException { //throws FileNotFoundException, IllegalArgumentException { checkDir(self); // TODO GroovyDoc doesn't parse this file as our java.g doesn't handle this JDK7 syntax try (DirectoryStream<Path> stream = Files.newDirectoryStream(self)) { for (Path path : stream) { if (fileType == FileType.ANY || (fileType != FileType.FILES && Files.isDirectory(path)) || (fileType != FileType.DIRECTORIES && Files.isRegularFile(path))) { closure.call(path); } } } }
/** * Processes each descendant file in this directory and any sub-directories. * Processing consists of potentially calling <code>closure</code> passing it the current * file (which may be a normal file or subdirectory) and then if a subdirectory was encountered, * recursively processing the subdirectory. Whether the closure is called is determined by whether * the file was a normal file or subdirectory and the value of fileType. * * @param self a Path (that happens to be a folder/directory) * @param fileType if normal files or directories or both should be processed * @param closure the closure to invoke on each file * @throws java.io.FileNotFoundException if the given directory does not exist * @throws IllegalArgumentException if the provided Path object does not represent a directory * @since 2.3.0 */ public static void eachFileRecurse(final Path self, final FileType fileType, @ClosureParams(value = SimpleType.class, options = "java.nio.file.Path") final Closure closure) throws IOException { // throws FileNotFoundException, IllegalArgumentException { // throws FileNotFoundException, IllegalArgumentException { checkDir(self); try (DirectoryStream<Path> stream = Files.newDirectoryStream(self)) { for (Path path : stream) { if (Files.isDirectory(path)) { if (fileType != FileType.FILES) closure.call(path); eachFileRecurse(path, fileType, closure); } else if (fileType != FileType.DIRECTORIES) { closure.call(path); } } } }
checkDir(self); try (DirectoryStream<Path> stream = Files.newDirectoryStream(self)) { Iterator<Path> itr = stream.iterator();
private static FileVisitResult traverse(final Path self, final Map<String, Object> options, @ClosureParams(value = SimpleType.class, options = "java.nio.file.Path") final Closure closure, final int maxDepth) throws IOException { checkDir(self); final Closure pre = (Closure) options.get("preDir"); final Closure post = (Closure) options.get("postDir");