java: search file according to its name in directory and subdirectories

前端 未结 5 1003
庸人自扰
庸人自扰 2021-02-03 13:50

I need a to find file according to its name in directory tree. And then show a path to this file. I found something like this, but it search according extension. Could anybody h

5条回答
  •  醉梦人生
    2021-02-03 14:17

    public static File find(String path, String fName) {
        File f = new File(path);
        if (fName.equalsIgnoreCase(f.getName())) return f;
        if (f.isDirectory()) {
            for (String aChild : f.list()) {
                File ff = find(path + File.separator + aChild, fName);
                if (ff != null) return ff;
            }
        }
        return null;
    }
    

提交回复
热议问题