I'm not sure if I'm using the right approach so I'm open to new ideas before telling me that i'm doing it wrong.
I have an array of directories path that I need to look for files. Let's say all .txt at example. Now I run a Files.walkFileTree(...)
on every directory and with the SimpleFileVisitor
that stops at first match. But now i want to add a next
button that keeps searching from the point I stop. How can I do this?
I've thought that I could save all matches on a array and then read it from there but it's space and memory consuming. So a better idea would be appreciate.
// example paths content: [/usr, /etc/init.d, /home]
ArrayList<String> paths;
for( String s : paths )
Files.walkFileTree(Paths.get(s), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (alreadyfound >= 10) {
return FileVisitResult.TERMINATE;
}
if (file.toString().endsWith(".txt")) {
System.out.println("Found: " + file.toFile());
}
return FileVisitResult.CONTINUE;
}
});
I once wrote a class that should do exactly what you described. I solved it by running the
FileVisitor
in its own Thread. When a file was found with a desired extension it simply stopped execution withwait()
until a button signaled to continue withnotify()
.A simple example how to use it would be this