简体   繁体   中英

Java code to list in all files from a directory with a particular word in filename

I have a directory with huge number of files,I need to list all the files from that directory with a particular word on it's name,like India or else. Also how to get the path of that files?

Something like this should work:

try (Stream<Path> paths = Files.walk(Paths.get("/home/you/Desktop"))) {
    paths
        .filter(Files::isRegularFile)
        // You can also use Apache StringUtils.containsIgnoreCase()
        .filter(path -> path.getFileName().toString().toLowerCase().contains(word.toLowerCase()))
        .forEach(System.out::println);
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM