Swingworker with FileVisitor class and walkFileTree(), which iterates internally over a “set” of files in a directory tree; where to publish()?

前端 未结 3 1591
萌比男神i
萌比男神i 2021-01-29 02:15

It seems like the long-running tree walker task should be defined in a class like so:

  public class TreeWalker extends SwingWorker implements         


        
3条回答
  •  Happy的楠姐
    2021-01-29 03:04

    Because your TreeWalker extends both SwingWorker and implements FileVisitor, you could be able to call publish from within any of the call back methods, for example...

    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
        publish(dir.toString());
        return FileVisitResult.CONTINUE;
    }
    

    Now, based on your needs, you would need to convert the Path element to a String using what ever method you need...

    Updated with working example

    import java.io.IOException;
    import java.nio.file.FileVisitResult;
    import java.nio.file.FileVisitor;
    import java.nio.file.Files;
    import java.nio.file.Path;
    import java.nio.file.Paths;
    import java.nio.file.attribute.BasicFileAttributes;
    import java.util.List;
    import java.util.concurrent.ExecutionException;
    import javax.swing.SwingWorker;
    
    public class TreeWalkerExample {
    
        public static void main(String[] args) {
            new TreeWalkerExample();
        }
    
        public TreeWalkerExample() {
            TreeWalker tw = new TreeWalker();
            tw.execute();
            try {
                tw.get();
            } catch (InterruptedException | ExecutionException ex) {
                ex.printStackTrace();
            }
        }
    
        public class TreeWalker extends SwingWorker implements FileVisitor {
    
            @Override
            protected void process(List chunks) {
                for (Path p : chunks) {
                    System.out.println(p);
                }
            }
    
            @Override
            protected Void doInBackground() throws Exception {
                Path p = Paths.get(System.getProperty("user.home"));
                System.out.println(p);
                Files.walkFileTree(p, this);
                return null;
            }
    
            @Override
            public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                FileVisitResult fvr = FileVisitResult.CONTINUE;
                if (dir.getFileName().toString().startsWith(".")) {
                    fvr = FileVisitResult.SKIP_SUBTREE;
                }
                return fvr;
            }
    
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                publish(file);
                return FileVisitResult.CONTINUE;
            }
    
            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return FileVisitResult.TERMINATE;
            }
    
            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                return FileVisitResult.CONTINUE;
            }
        }
    
    }
    

    Nb, this doesn't have a GUI with it, but instead waits for the worker to complete by waiting for get to return is only meant as an example

提交回复
热议问题