I use the following lambda expression to iterate over PDF files.
public static void run(String arg) {
Path rootDir = Paths.get(arg);
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:**.pdf");
Files.walk(rootDir)
.filter(matcher::matches)
.forEach(Start::modify);
}
private static void modify(Path p) {
System.out.println(p.toString());
}
This part .forEach(Start::modify);
executes the static method modify from the same class where the lambda expression is located. Is there a possibility to add something like else
clause when no PDF file is found?
Or you could just do the obvious thing, which is collect the stream first.
List<File> files = Files.walk(rootDir)
.filter(matcher::matches)
.collect(toList());
if (files.isEmpty())
doSomethingForEmpty();
else
files.forEach(Start::modify);
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