当前位置: 首页 > 面试题库 >

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

包唯
2023-03-14
问题内容

似乎长时间运行的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 ......


问题答案:

因为您同时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返回来等待工作人员完成只是一个示例



 类似资料:
  • 我们在MapStruct中使用不可变项,在将实体转换为dto时遇到问题。 mapper类 在出现错误的情况下,问题是完全相同的 我检查了mapstruct与不可变的测试,没有什么不同https://github.com/mapstruct/mapstruct/blob/master/integrationtest/src/test/resources/immutablesBuilderTest/m

  • 问题内容: 我只是想知道内部和外部迭代的真正好处是什么,以及为什么使用内部操作更好(至少是我所听到的)。在对集合进行内部迭代时,是否还可以删除集合的元素?就像在代码示例中一样: 我知道内部迭代的代码可读性更好,但是还有其他一些好处,例如性能改进? 问题答案: 您的情况有些简单,因为您可以简单地使用resp。而是使用内部迭代的替代方法,它也可以处理更复杂的条件。 在的情况下,这将立即显示内部迭代的优

  • 问题内容: 在java中内部类和静态内部类有什么不同? 问题答案: 从Java教程: 嵌套类分为两类:静态和非静态。声明为静态的嵌套类简称为静态嵌套类。非静态嵌套类称为内部类。 静态嵌套类使用封闭的类名称访问: 例如,要为静态嵌套类创建一个对象,请使用以下语法: 作为内部类实例的对象存在于外部类实例中。考虑以下类别: InnerClass的实例只能存在于OuterClass的实例中,并且可以直接访

  • 我必须在Java中实现一个名为的接口。 假设是我编写的实现graph的类。 我想创建一个内部类以便“封装”类型的元素。 问题是我不明白内部类应该是还是、还是以及它的实例变量应该声明为还是。对于我将要在中插入的一些最终方法也是如此。 基本上,类节点应该像C语言中的记录类型(带有),并且外部类应该能够访问节点的所有实例变量,而不需要观察器、getter等。

  • 有内在的 科特林拥有的: Java如何解决这个问题:

  • 问题内容: 对于这些查询,我可以获得相同的结果,但是哪一个是最快,最有效的? in()或内部联接在哪里? 和 问题答案: 取决于您的SQL引擎。具有合理查询优化器的较新SQL系统很可能会将两个查询重写为同一计划。通常,使用联接(第一个查询)重写子查询(第二个查询)。 在可能没有出色查询优化器的简单SQL引擎中,联接应该更快,因为它们可能在运行外部查询之前将子查询运行到临时内存表中。 但是,在某些内