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?
Asked
Active
Viewed 76 times
-1
-
You can write your own `FileFilter` - in particular, you'll place your implementation when you @Override `accept()` – sleepToken Jan 21 '20 at 16:36
-
https://docs.oracle.com/javase/8/docs/api/java/io/File.html#listFiles-java.io.FilenameFilter- – JB Nizet Jan 21 '20 at 16:36
1 Answers
2
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);
}

Alejandro Bentivengo
- 46
- 4