gpt4 book ai didi

java - 搜索在特定时间完成

转载 作者:行者123 更新时间:2023-12-02 06:18:23 25 4
gpt4 key购买 nike

我有一个代码可以在特定位置搜索文件。我想添加停止搜索的时间限制。如果我说在 5000 毫秒内搜索,那么搜索必须在 5000 毫秒后停止并显示结果。谁能建议最好的方法吗?

public class FileSearch {

final private int FOLDER=1,FILE=2,BOTH=3;
private Pattern pat;
private int filter;
private List<String> searchList = new ArrayList<String>();
public void setFilter(int filter) {
this.filter = filter;
}

public void setPat(Pattern pat) {
this.pat = pat;
}

private void findFileNames(File file){
File[] list = file.listFiles();
if (list != null)
for (File fil : list) {
if (fil.isDirectory()) {
findFileNames(fil);
} else {
matchName(fil.getName());
}
}
}
private void findFolderNames(File file){
File[] list = file.listFiles();
if (list != null)
for (File fil : list) {
if (fil.isDirectory()) {
matchName(fil.getName());
findFolderNames( fil);
}
}
}
public void findFile(File file) {

switch(this.filter){
case FOLDER:
findFolderNames(file);
break;
case FILE:
findFileNames(file);
break;
case BOTH:

File[] list = file.listFiles();
if (list != null)
for (File fil : list) {
if (fil.isDirectory()) {
findFolderNames(fil);
} else {
matchName(fil.getName());
}
}

findFileNames(file);
findFolderNames(file);
break;
default:
break;

}

}

public void startFileSearch() {
FileSearch ff = new FileSearch();
Scanner scan = new Scanner(System.in);
System.out.println("Enter the file name to be searched (Can use regular expressions).. ");
String name = scan.next();
System.out.println("Enter the directory where to search ");
String directory = scan.next();
System.out.println("Enter filter (1.FOLDER/2..FILE/3.BOTH) to search ");
int filter = scan.nextInt();
ff.setPat(Pattern.compile(name));
ff.setFilter(filter);
ff.findFile(new File(directory));
}

private void matchName(String fileName){
if (this.pat.matcher(fileName).find()) {
System.out.println(fileName);
this.searchList.add(fileName);
}

}
}

最佳答案

我建议使用 Quartz API。但您也可以使用 javas Timer 和 TimerTask API。您可以使用以下引用:Timer & Timertask

关于java - 搜索在特定时间完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21249241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com