Java在某一文件夹下遍历查找某一文件、
直接贴代码。
static boolean result = false;
static String filepath = "";
private static void findFile(String path, String filename) throws IOException {
File file = new File(path);
File[] tempList = file.listFiles();
System.out.println("该目录下对象个数:" + tempList.length);
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile() && filename.equals(tempList[i].getName())) {
System.out.println("已找到该文件。");
filepath = path + "\\";
result = true;
} else if (tempList[i].isDirectory()) {
// 读取某个文件夹下的所有文件夹
System.out.println("读取某个文件夹下的所有文件夹");
findFile(tempList[i].getParent() + "\\" + tempList[i].getName(), filename);
// System.out.println("文件夹:"+tempList[i].getParent()+"\\"+tempList[i].getName());
}
}
}