How to use Files.walk()… to get a graph of files based on conditions?

前端 未结 3 1813
名媛妹妹
名媛妹妹 2021-02-01 04:24

I have the following directory structures:

/path/to/stuff/org/foo/bar/
/path/to/stuff/org/foo/bar/1.2.3/
/path/to/stuff/org/foo/bar/1.2.3/myfile.ext
/path/to/stu         


        
3条回答
  •  遥遥无期
    2021-02-01 05:17

    Files.walk(Paths.get("/path/to/stuff/"))
         .filter(p -> p.toString().endsWith(".ext"))
         .map(p -> p.getParent().getParent())
         .distinct()
         .forEach(System.out::println);
    

    This filters all files that have the extension and gets the parent path of their directory. distinct ensures that every path is used only once.

提交回复
热议问题