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?
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.
String path = context. getFilesDir(). getAbsolutePath(); File file = new File(path + "/filename");
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) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With