fileVisitor
@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:
- FileVisitor.preVisitDirectory returns FileVisitResult.CONTINUE.
- FileVisitor.visitFile returns FileVisitResult.CONTINUE.
- FileVisitor.visitFileFailed re-throws the I/O exception that prevented the file from being visited.
- FileVisitor.postVisitDirectory returns FileVisitResult.CONTINUE if the directory iteration completes without an I/O exception; otherwise it re-throws the I/O exception that caused the iteration of the directory to terminate prematurely.
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
}
}