I'm writing a java program that needs to find a file directory only having the file name. For example, I know the file is called “Img_123.png” but I do not know it's file directory, file name, or file path. I need to know the entire file name in as a string(in order to draw it using Canvas).
Provided root folder to start your search and the max allowed depth, this should return list of all files that match the filename:
List<File> findFileByName(Path start, String fileName, int maxDepth) throws IOException {
final Stream<Path> files = Files.find(start, maxDepth, (path, attribute) -> {
return path.endsWith(fileName);
});
return files
.map(path -> path.toFile())
.collect(Collectors.toList());
}
(path, attribute) -> { //any code }
that you're confused about is a lambda expression an anonymous function . It's type is BiPredicate<Path,BasicFileAttributes>
which means it should return a Boolean based on some evaluation of the path and attributes for the file being processed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.