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 subdirectories.
In Java 8 via the streams API:
Optional<Path> 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.
To walk the file tree, FileVisitor interface can be used. Please see the tutorial. Please see Find sample codes also.
Your solution will include the use of File.listFiles(String)
java.io.File
API reference
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With