walk
Returns a sequence of paths for visiting this directory and all its contents.
By default, the returned sequence includes only files in the file tree, in depth-first order. Symbolic links are not followed; if encountered, they are included in the sequence as-is, and the contents of the directories they point to are not visited. The combination of options overrides this default behavior. See PathWalkOption for details.
The order in which sibling entries are visited is unspecified.
If, after calling this function, new entries get added or deleted from the file tree rooted at this directory, the changes may or may not be reflected in the returned sequence.
If the entry located by this path does not exist, an empty sequence is returned. If the entry located by this path is not a directory, a sequence containing only this path is returned.
Example:
val startDirectory = createTempDirectory()
run {
(startDirectory / "1" / "2" / "3" / "4").createDirectories()
(startDirectory / "1" / "2" / "3" / "a.txt").createFile()
(startDirectory / "1" / "2" / "b.txt").createFile()
(startDirectory / "c.txt").createFile()
}
// Default walk options. Prints:
// 1/2/b.txt
// 1/2/3/a.txt
// c.txt
startDirectory.walk().forEach { path ->
println(path.relativeTo(startDirectory))
}
// Custom walk options. Prints:
// 1
// c.txt
// 1/2
// 1/2/b.txt
// 1/2/3
// 1/2/3/a.txt
// 1/2/3/4
startDirectory.walk(PathWalkOption.INCLUDE_DIRECTORIES, PathWalkOption.BREADTH_FIRST).forEach { path ->
println(path.relativeTo(startDirectory))
}
When iterating the returned sequence, the following exceptions could be thrown:
FileSystemException if the traversal reaches an entry with an illegal name such as "." or "..".
FileSystemLoopException if the traversal reaches a cycle.
SecurityException if a security manager is installed and it denies access to an entry reached during traversal.
IOException if any errors arise while opening a directory.