62

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?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
matteo
  • 2,121
  • 4
  • 20
  • 33

1 Answers1

95

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) }
hotkey
  • 140,743
  • 39
  • 371
  • 326