似乎应该在这样的类中定义长期运行的树遍历任务:
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()在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()内部的一个类中,但这似乎是一厢情愿的想法。
P.S.我不能使用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分钟后发布)
这是我的处理方法。王子没有执行。
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()=============================
..。又一个该死的编辑..。我现在的班级.
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..。5.
发布于 2013-11-19 00:16:34
因为您的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;
}
}
}注意,它没有图形用户界面,而是通过等待get返回来等待工人完成,这只是一个例子
发布于 2013-11-19 23:02:18
由于@Mad程序员没有使用GUI,并且确实使用了get(),直到doInBackground()的执行完成,所以我添加了一个GUI,修改了他的发布(),并包含了对done()的调用,就像锦上添花一样。我自己的爬树器还不起作用,但疯子给我指明了方向。以下是新的Mad with-GUI版本的亮点。
public class TreeWalkerExample {
static GUI gui;
public static void main(String args[])
{...invokelater...
public void run() {
gui = new GUI();
gui.setVisible(true); }
}
public TreeWalkerExample() {
(new TreeWalker()).execute();
}
public class TreeWalker extends SwingWorker<Void,Path> implements FileVisitor<Path> {
protected Void doInBackground() throws Exception {
Path p = Paths.get("C:\\","Users","\\Dave","\\Documents","\\Java");
gui.appendOutput(p.toString());
Files.walkFileTree(p, this);
return null;
}
public FileVisitResult visitFile(Path file, BasicFileAttributes a) throws IOException{
publish(file);
return FileVisitResult.CONTINUE;
}
protected void process(List<Path> chunks) {
for (Path p : chunks)
gui.appendOutput(p.getFileName().toString());
}
protected void done(){
gui.appendOutput("\nDone");
}
}
===================================================================================================
public class GUI extends javax.swing.JFrame {
JTextArea output;
private void btnWalkMouseClicked(java.awt.event.MouseEvent evt) {
new TreeWalkerExample();
}
public void appendOutput(String s){
output.append("\n" + s);
}发布于 2013-12-08 02:46:41
这并不是我放弃了SwingWorker,我只是确定我不知道线程,并决定对它做些什么。在过去的两天里,我成功地完成了一个简单得多的项目,这让我把同样的策略应用到了我的(各种) Treewalker (项目)上,现在它是:(1)当输出附加到文本区域时,不要让屏幕闪烁;(2)以按钮键结束。
它只需使用一个单独的线程(而不是SwingWorker)来执行“后台”FileVisitor任务,该任务是:(a)让GUI“负责”,从而能够无缝地接受输出,并为用户提供一个按钮来按下中止;(b)使代码看起来正常,易于跟踪。
所以@Mad,再次感谢您的帮助。(从11月19日起,我就没有单独做过这个工作了!我感到非常沮丧,我只是离开了它,成功地做了其他的事情,并且鼓起勇气回来再试一次)。
我发现伊瓦尔·霍顿的开始Java (7)是非常宝贵的。我见过的关于线的最好的。
FWIW以下是我的备份程序概要:
public class Copy extends Thread{
public static FileVisitResult disposition = FileVisitResult.CONTINUE;
static Thread t ;
static FilesCopied output ;
...
public static TreeWalker fv;
...
public void run() {
...
fv = new TreeWalker();
try {
Files.walkFileTree(UserIO.inputPath,fv);
}
catch ...
}
public /* inner */ class TreeWalker implements FileVisitor<Path> {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
maybeCopy(file);
return disposition;
}
public FileVisitResult preVisitDirectory(Path d, BasicFileAttributes a) throws IOException {
maybeMakeDir(d,fromRootDepth);
return disposition;
}
...
} // end TreeWalker
...
public static void main(String[] args) throws IOException {
EventQueue.invokeLater(new Runnable()
public void run() { gui = new UserIO(); gui.setVisible(true);
}});
EventQueue.invokeLater(new Runnable() {
public void run() {
output = new FilesCopied();
}});
t = new Copy();
}
} // end class Copy
======================
public class UserIO extends JFrame {
...
public void btnBackupMouseClicked(MouseEvent evt) throws IOException {
...
Copy.t.start();
}
public void btnStopMouseClicked(MouseEvent evt) throws IOException {
Copy.disposition = FileVisitResult.TERMINATE;
}
}https://stackoverflow.com/questions/20060366
复制相似问题