1 package com.qianfeng.Demo;
2
3 import java.io.File;
4 import java.util.Scanner;
5
6 @SuppressWarnings("all")
7 public class Work1 {
8 public static void main(String[] args) throws Exception {
9 Scanner sc = new Scanner(System.in);
10 System.out.println("输入你要搜索的文件:");
11 String name = sc.next();
12 find(name);
13 // find(new File("E:/"),name);
15 }
16 public static void find(String name) {
17 File[] roots = File.listRoots();
18 for (File root : roots) {
19 find(root,name);
20 }
21 }
22 public static void find(File root,String f) {
23 File[] files = root.listFiles();
24 if(files==null) {
25 return;
26 }
27 for (File fs : files) {
28 if(fs.isDirectory()) {
29 find(fs,f);
30 continue;
31 }
//字符串搜索或者正则表达式搜索
32 if(fs.getName().toLowerCase().indexOf(f.toLowerCase()) > -1
33 || fs.getName().matches(f)) {
34 System.out.println("绝对路径为:"+fs.getAbsolutePath());
35 }
36 }
37 }
38 }