Windows自带搜索引擎太慢?那就来试试这个用java做的多线程搜索程序吧!
众所周知,windows自带的搜索引擎的速度着实感人,所以大多数人都会选择想Everything这样的搜索软件,这几天就突然突发奇想,为何不自己写一个搜索软件呢?
说干就干!充分发挥java多线程的优势来实现超快的搜索速度!
首先先写一个搜索的线程
class select extends Thread {
private String file;
private String find;
public select(String file, String find) {
this.file = file;
this.find = find;
}
@Override
public void run () { //这是核心线程
try {
File file = new File(this.file);
File result[] = file.listFiles();
for (int x = 0 ; x < result.length ;x ++ ) {
String a = "" + result[x];
if (a.contains(this.find)) {
System.out.println(result[x]);
//输出文件路径
}
if (result[x].isFile()) {
} else {
new select(a , this.find).start();
}
}
} catch(Exception e) {
}
}
}
原理就是判断它是不是文件,若不是则再次创建一个新的线程来进行查询,
再加上主类
public class FileSelect {
public static void main(String args[]) throws IOException {
var file = "D:\\";
while (true) {
System.out.println("输入指令:");
InputStream input = System.in;
byte[] data = new byte[1024];
int len = input.read(data);
String i = new String(data, 0, len);
try {
if (i.contains("use")) {
String result[] = i.split(" ");
file = result[1];
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(file);
file = m.replaceAll("");
System.out.println(file);
System.out.println("Disk changed!");
} else if (i.contains("select ")) {
String result[] = i.split(" ");
String find = result[1];
//要去除换行符的字符串
Pattern p = Pattern.compile("\\s*|\t|\r|\n");
Matcher m = p.matcher(find);
find = m.replaceAll("");
System.out.println(find);
new select(file, find).start();
} else {
System.out.println("未知指令!");
}
} catch(Exception e) {
System.out.println("未知指令!");
}
}
}
}
最后来测试一下速度!
创建一个txt文件
C:\ProgramFiles(x86)\Bloody7\Bloody7\Data\Mouse\ChineseS\DefaultModel\12345678.txt
怎么样,是不是非常快!QAQ
ps:这个耗电脑cpu,所以使用时尽量不要点其他的