Continue a visit of SimpleFileVisitor

296 Views Asked by At

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;
        }
    });
1

There are 1 best solutions below

2
On BEST ANSWER

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 with wait() until a button signaled to continue with notify().

public class FileSearcher extends Thread{
    private Object lock = new Object();
    private Path path;
    private JLabel label;
    private String extension;

    public FileSearcher(Path p, String e, JLabel l){
        path = p;
        label = l;
        extension = e;
    }       
    public void findNext(){
        synchronized(lock){
            lock.notify();
        }   
    }   
    @Override
    public void run() {
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    if(file.toString().toLowerCase().endsWith(extension)){
                        label.setText(file.toString());
                        synchronized(lock){
                            try {
                                lock.wait();
                            } catch (InterruptedException e1) {
                                e1.printStackTrace();
                            }
                        }
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }       
    }
}

A simple example how to use it would be this

JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel();
FileSearcher fileSearcher = new FileSearcher(Paths.get("c:\\bla"), ".txt", label);
JButton button = new JButton();
button.setText("next");
button.addActionListener(new ActionListener(){
    @Override
    public void actionPerformed(ActionEvent arg0) {
        fileSearcher.findNext();
    }});
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
fileSearcher.start();