fileVisitor

JVM
JRE7
1.7
@ExperimentalPathApi fun fileVisitor(
    builderAction: FileVisitorBuilder.() -> Unit
): FileVisitor<Path>

(source)

Builds a FileVisitor whose implementation is defined in builderAction.

By default, the returned file visitor visits all files and re-throws I/O errors, that is:

To override a function provide its implementation to the corresponding function of the FileVisitorBuilder that was passed as a receiver to builderAction. Note that each function can be overridden only once. Repeated override of a function throws IllegalStateException.

The builder is valid only inside builderAction function. Using it outside the function throws IllegalStateException.

Example:

val cleanVisitor = fileVisitor {
    onPreVisitDirectory { directory, _ ->
        if (directory.name == "build") {
            directory.toFile().deleteRecursively()
            FileVisitResult.SKIP_SUBTREE
        } else {
            FileVisitResult.CONTINUE
        }
    }

    onVisitFile { file, _ ->
        if (file.extension == "class") {
            file.deleteExisting()
        }
        FileVisitResult.CONTINUE
    }
}