Recursively list files in Java

后端 未结 27 2160
走了就别回头了
走了就别回头了 2020-11-22 00:29

How do I recursively list all files under a directory in Java? Does the framework provide any utility?

I saw a lot of hacky implementations. But none from the fra

27条回答
  •  醉梦人生
    2020-11-22 01:00

    Kotlin has FileTreeWalk for this purpose. For example:

    dataDir.walkTopDown().filter { !it.isDirectory }.joinToString("\n") {
       "${it.toRelativeString(dataDir)}: ${it.length()}"
    }
    

    Will produce a text list of all the non-directory files under a given root, one file per line with the path relative to the root and length.

提交回复
热议问题