Windows自带搜索引擎太慢?那就来试试这个用java做的多线程搜索程序吧!

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,所以使用时尽量不要点其他的

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Spasol

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值