简单的文件搜索

class FileSeek {

	private static File[] arr = new File[1024 * 512];

	static int p = -1;

	public boolean isEmpty() {// 判断是否为空
		if (p > -1)
			return false;
		else
			return true;
	}

	public void push(File obj) {// 压栈
		p++;
		arr[p] = obj;
	}

	public File pop() {// 出栈
		if (p < 0) {
			try {
				throw new Exception("空堆栈");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		p--;
		return arr[p + 1];
	}
}

public class FileSeekTest {

	public static void FunFind(String driver, String finds) {// 查找文件
		FileSeek seek = new FileSeek();

		File di = new File(driver);
		seek.push(di);
		boolean f = false;
		lab: while (!seek.isEmpty()) {
			// 出堆栈
			File cur = seek.pop();
			File[] dir = cur.listFiles();
			for (File dif : dir) {
				if (!dif.isHidden()) {
					if (dif.isDirectory()) {// 文件夹入堆栈
						seek.push(dif);
					}
					if (dif.isFile()) {
						if (dif.getName().toLowerCase().equals(
								finds.toLowerCase())) {// 判断是否为该文件
							System.err
									.println("找到文件\t" + dif.getAbsolutePath());
							f = true;
							break lab;
						}
						System.out.println(dif.getAbsolutePath());
					}
				}
			}
		}
		if (f == false) {
			System.out.println("没有找到该文件!");
		}
	}

	public static void main(String[] args) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		try {
			System.out.println("请输入要查找的文件名:");
			String find = br.readLine();
			long start = System.currentTimeMillis();
			FileSeekTest.FunFind("f:\\", find);
			long end = System.currentTimeMillis() - start;
			System.out.println("用时" + end + "毫秒");

		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值