微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Swingworker具有FileVisitor类和walkFileTree,可在目录树中的一组“文件”内部进行内部迭代;在哪里发布?

如何解决Swingworker具有FileVisitor类和walkFileTree,可在目录树中的一组“文件”内部进行内部迭代;在哪里发布?

因为您同时TreeWalker扩展SwingWorker和实现了FileVisitor,所以您可以publish从任何回调方法中进行调用,例如…

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

现在,根据需要,您将需要使用所需的任何方法Path元素转换为String…。

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<Void, Path> implements FileVisitor<Path> {

        @Override
        protected void process(List<Path> 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,它没有GUI,但是通过等待get返回来等待工作人员完成只是一个示例

解决方法

似乎长时间运行的tree walker任务应在如下类中定义:

  public class TreeWalker extends SwingWorker<Void,String> implements FileVisitor<Path>

并开始这样的地方:

TreeWalker walker = (new TreeWalker());
           walker.execute();

长时间运行的任务不仅由类中的方法的 一次 调用发起,而且 完全 由其 单独 执行
。因此,一定要拨入它。walkFileTree()``Files``doInBackGround()

  protected Void doInBackground() throws Exception {
    Files.walkFileTree(SearchyGUI.p,this);
    return null;
  }

请注意,walkTreeFile() 内部会 为遇到的每个文件调用四个方法。程序员编写的循环是不可行的。 所以这是我的问题。
如何publish()将文件信息作为字符串发送到process需要重写的方法?我见过的示例具有publish()inside
doInBackground(),但位于循环内,在这里是不可能的。

我最关心的四种方法之一是visitFile(),它walkFileTree()必须能够找到,我怀疑这是放置在哪里publish()

  public FileVisitResult visitFile(Path f,BasicFileAttributes a) throws IOException {
    if (...we want this file...) 
        publish(f.toString());
    return CONTINUE;
  }

我可以将walkFileTree()调用的所有4种方法放在一个内部类中doInBackground(),但这似乎是一厢情愿的想法。

PS我无法使用get();
这就是重点(据我所知)-获取结果的延迟太多(可能要处理数千个文件才能找到十几个文件),而要等到doInBackground()结束。

=========================================

编辑#3,原始发布时间后50分钟

  public static void doIt(){
      try {
        System.out.println("It begins..."); // This does happen.
        TreeWalker walker = new TreeWalker();
        walker.execute(); 
        SearchyGUI.info.setVisible(true); // Form is displayed,stays blank.
      }      
      catch (Exception e) { System.out.println("Uh-oh"); } // This does NOT happen.
    }

=========================================

(编辑2,发布40分钟后)

这是我的处理方法。println没有执行。

protected void process(String s) {
    System.out.println("in process()...");
    report(s); // my method to append text area with another line of file info
}

此外,包含的类语句doInBackground()已更改:

public class TreeWalker extends SwingWorker<Void,String> implements Runnable{

Walking级嵌套内doInBackground()

=========================================

(编辑,发布后20分钟)

编译后却什么也没做:

  protected Void doInBackground() throws Exception 
  {
    class Walking implements FileVisitor<Path>
    {  
      @Override
      public FileVisitResult visitFile(Path f,BasicFileAttributes a) throws IOException 
      {
        String modifyDate    = a.lastModifiedTime().toString().substring(0,10);
        String fpathname = f.toString();// + "\\" + f.getFileName().toString());
        if (...we want this one...) 
            publish(f.getFileName());
        return disposition;
      }
... other methods excluded
    } // end inner class

    System.out.println("walking??");                                 // We get here ...
    Files.walkFileTree(SearchyGUI.p,(FileVisitor<? super Path>) this);
    System.out.println("Finished walking??");                        // ... but not here.
    return null;
  } // end of doInBackground()

============================

…另一个freakin的编辑…我当前的班级防御…

public class GUI extends JFrame implements ActionListener,MouseListener,KeyListener

public class TreeWalker extends SwingWorker<Void,String> implements Runnable{

protected Void doInBackground() throws Exception {
  class Walking implements FileVisitor<Path>{ // CLASS INSIDE doInBackground

… zzzzzzzzzzzzzzzzzzzzzz ......

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。