递归列出Kotlin中的文件 [英] List files recursively in Kotlin

查看:234
本文介绍了递归列出Kotlin中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用kotlin列出目录中的文件,我使用了list()和listFiles()函数:

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?

推荐答案

使用 FileTreeWalk ,它实现了 Sequence<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) }

这篇关于递归列出Kotlin中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆