因为您同时
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返回来等待工作人员完成只是一个示例
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)