继续访问 SimpleFileVisitor
Continue a visit of SimpleFileVisitor
我不确定我是否使用了正确的方法,所以在告诉我我做错之前我对新想法持开放态度。
我有一组目录路径,我需要查找文件。让我们以所有 .txt 为例。现在我 运行 每个目录上的 Files.walkFileTree(...)
和 SimpleFileVisitor
在第一次匹配时停止。但现在我想添加一个 next
按钮,从我停止的那一刻开始继续搜索。我该怎么做?
我想我可以将所有匹配项保存在一个数组中,然后从那里读取它,但它 space 并且很耗内存。如果有更好的主意,我们将不胜感激。
// example paths content: [/usr, /etc/init.d, /home]
ArrayList<String> paths;
for( String s : paths )
Files.walkFileTree(Paths.get(s), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (alreadyfound >= 10) {
return FileVisitResult.TERMINATE;
}
if (file.toString().endsWith(".txt")) {
System.out.println("Found: " + file.toFile());
}
return FileVisitResult.CONTINUE;
}
});
我曾经写过一个 class 应该完全按照你描述的那样做。我在它自己的线程中通过 运行 FileVisitor
解决了它。当找到具有所需扩展名的文件时,它会简单地使用 wait()
停止执行,直到一个按钮发出继续 notify()
.
的信号
public class FileSearcher extends Thread{
private Object lock = new Object();
private Path path;
private JLabel label;
private String extension;
public FileSearcher(Path p, String e, JLabel l){
path = p;
label = l;
extension = e;
}
public void findNext(){
synchronized(lock){
lock.notify();
}
}
@Override
public void run() {
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(file.toString().toLowerCase().endsWith(extension)){
label.setText(file.toString());
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
如何使用它的一个简单示例就是这个
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel();
FileSearcher fileSearcher = new FileSearcher(Paths.get("c:\bla"), ".txt", label);
JButton button = new JButton();
button.setText("next");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
fileSearcher.findNext();
}});
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
fileSearcher.start();
我不确定我是否使用了正确的方法,所以在告诉我我做错之前我对新想法持开放态度。
我有一组目录路径,我需要查找文件。让我们以所有 .txt 为例。现在我 运行 每个目录上的 Files.walkFileTree(...)
和 SimpleFileVisitor
在第一次匹配时停止。但现在我想添加一个 next
按钮,从我停止的那一刻开始继续搜索。我该怎么做?
我想我可以将所有匹配项保存在一个数组中,然后从那里读取它,但它 space 并且很耗内存。如果有更好的主意,我们将不胜感激。
// example paths content: [/usr, /etc/init.d, /home]
ArrayList<String> paths;
for( String s : paths )
Files.walkFileTree(Paths.get(s), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (alreadyfound >= 10) {
return FileVisitResult.TERMINATE;
}
if (file.toString().endsWith(".txt")) {
System.out.println("Found: " + file.toFile());
}
return FileVisitResult.CONTINUE;
}
});
我曾经写过一个 class 应该完全按照你描述的那样做。我在它自己的线程中通过 运行 FileVisitor
解决了它。当找到具有所需扩展名的文件时,它会简单地使用 wait()
停止执行,直到一个按钮发出继续 notify()
.
public class FileSearcher extends Thread{
private Object lock = new Object();
private Path path;
private JLabel label;
private String extension;
public FileSearcher(Path p, String e, JLabel l){
path = p;
label = l;
extension = e;
}
public void findNext(){
synchronized(lock){
lock.notify();
}
}
@Override
public void run() {
try {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if(file.toString().toLowerCase().endsWith(extension)){
label.setText(file.toString());
synchronized(lock){
try {
lock.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
如何使用它的一个简单示例就是这个
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
JLabel label = new JLabel();
FileSearcher fileSearcher = new FileSearcher(Paths.get("c:\bla"), ".txt", label);
JButton button = new JButton();
button.setText("next");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
fileSearcher.findNext();
}});
panel.add(label);
panel.add(button);
frame.add(panel);
frame.setSize(300, 300);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
fileSearcher.start();