java如何判断给定目录下,是否存在指定的文件夹和文件

现有如下目录D:\test\test1\test11\test111.txt
请问如何使用java判断D:下是否存在test11和test111.txt

我写的这个只能判断出test11,txt文件就判断不出来了,请问该如何修改?
public class demo1 {
public static void main(String[] args) {
String dir = "D:\\test";
File file = new File(dir);
find(file);
}

public static void find(File f) {
if (f.isDirectory()) {
if (f.listFiles().length > 0) {
File[] files = f.listFiles();
for (File file : files) {
// System.out.println(file);
// System.out.println(file.getName());
// System.out.println(file.listFiles().length);
// System.out.println("=======================");

if (file.isFile()) {
if (file.getName().equals("test111.txt")) {
System.out.println(file);
}
} else {
if (file.getName().equals("test11")) {
System.out.println(file);

} else {
if (file.listFiles().length > 0) {
find(file);
}
}
}
}
}
}
}

}

public static void find(File f) {
    if (!f.isDirectory() || f.listFiles().length <= 0)
        return;
    File[] files = f.listFiles();
    for (File file : files) {
        if (file.isFile()) {
            //System.out.println("got: " + file.getName());
            if (file.getName().equals("test111.txt")) {
                System.out.println(file);
            }
        } else {
            //System.out.println("got: " + file.getName());
            if (file.getName().equals("test11")) {
                System.out.println(file);
            }
            find(file); // 这个不能放进else里
        }
    }
}

温馨提示:答案为网友推荐,仅供参考

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网