List files recursively in Kotlin

FileKotlinRecursionDirectory

File Problem Overview


to list files in a directory with kotlin, i used list() and listFiles() functions:

File("/tmp").list().forEach { println(it) }
File("/tmp").listFiles().forEach { println(it) }

but, how can i list files recursively?

File Solutions


Solution 1 - File

Use one of .walk(...), .walkBottomUp() or .walkTopDown() extensions for File, which differ only in the order in which the files appear and all produce a FileTreeWalk, that implements Sequence<File>:

File("/tmp").walkTopDown().forEach { println(it) }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmatteoView Question on Stackoverflow
Solution 1 - FilehotkeyView Answer on Stackoverflow