I'm trying to use more Java 8 syntax. I have a simple use case here, listing files recursively, where I'd like to print more than just the file name as the example shows:
public void listFiles(String path) {
try {
Files.walk(Paths.get(path))
.filter(p -> {
return Files.isRegularFile(p);
})
.forEach(System.out::println);
} catch (IOException e) {
e.printStackTrace();
}
}
Is there a way to call a method from the forEach method, passing the file in question as a parameter? How would I reference the file?
Edit: There was some confusion in the discussion about whether the path of each file being printed could be passed as a variable to another method.
To confirm, it can. Here's the code:
public void listFiles(String path) {
try {
Files.walk(Paths.get(path))
.filter(p -> {
return Files.isRegularFile(p);
})
.forEach(p -> myMethod(p));
} catch (IOException e) {
e.printStackTrace();
}
}
private void myMethod(Path path) {
System.out.println(path.toAbsolutePath());
try {
BasicFileAttributes attr = Files.readAttributes(path, BasicFileAttributes.class);
FileTime fileTime = attr.lastModifiedTime();
System.out.println("file date: " + fileTime);
} catch (IOException ex) {
// handle exception
}
}