Recursively list files in Java

后端 未结 27 2043
走了就别回头了
走了就别回头了 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 00:54

    In Java 8, we can now use the Files utility to walk a file tree. Very simple.

    Files.walk(root.toPath())
          .filter(path -> !Files.isDirectory(path))
          .forEach(path -> System.out.println(path));
    

提交回复
热议问题