package cn.itcast.level02.io.byteDemo; import java.io.File; import java.util.Scanner; public class SearchFileDemo { public static void main(String[] args) { String path = "D:"; Scanner scanner = new Scanner(System.in); System.out.println("请输入想要在D盘中找到的文件的文件名:"); String temp = scanner.next(); boolean b = searchFile(temp, path); if (!b) { System.out.println("没有找到"); } } public static boolean searchFile(String temp, String path) { File file = new File(path); File[] files = file.listFiles(); int i = 0; if (files == null) { return false; } System.out.println("初始"+i); for (;i < files.length;i++) { System.out.println("文件名"+files[i].getName()); System.out.println("循环"+i); /*int c = files[i].getName().lastIndexOf(".",+1);*/ if (files[i].getName().equals(temp)) { System.out.println("找到了\t"+path+File.separator+files[i].getName()); return true; }else { if (files[i].isDirectory()) { System.out.println(path+File.separator+files[i].getName()); System.out.println("================================"); boolean b = searchFile(temp, path + File.separator + files[i].getName()); if (b) { return true; } System.out.println("返回上级目录"); System.out.println(path+File.separator+files[i].getName()); } } } return false; } }