Recursively search for a directory in Java

后端 未结 7 2174
深忆病人
深忆病人 2021-01-05 10:45

What is the best way to find a directory with a specific name in Java? The directory that I am looking for can be located either in the current directory or one of its subdi

7条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-05 10:56

    In Java 8 via the streams API:

    Optional hit = Files.walk(myPath)
       .filter(file -> file.getFileName().equals(myName))
       .findAny();
    

    The #walk is lazy, so any short-circuiting terminal operation will optimize the IO required.

提交回复
热议问题