Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List files recursively in Kotlin

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?

like image 339
matteo Avatar asked Jun 15 '17 12:06

matteo


People also ask

How do I use a list on Kotlin?

Inside main() , create a variable called numbers of type List<Int> because this will contain a read-only list of integers. Create a new List using the Kotlin standard library function listOf() , and pass in the elements of the list as arguments separated by commas.

How do I find the file path in Kotlin?

String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");


1 Answers

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) } 
like image 86
hotkey Avatar answered Sep 19 '22 05:09

hotkey