Find files in a folder using Java

前端 未结 12 1942
日久生厌
日久生厌 2020-11-28 08:15

What I need to do if Search a folder say C:\\example

I then need to go through each file and check to see if it matches a few start characters so if fil

12条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-28 08:17

    You give the name of your file, the path of the directory to search, and let it make the job.

    private static String getPath(String drl, String whereIAm) {
        File dir = new File(whereIAm); //StaticMethods.currentPath() + "\\src\\main\\resources\\" + 
        for(File e : dir.listFiles()) {
            if(e.isFile() && e.getName().equals(drl)) {return e.getPath();}
            if(e.isDirectory()) {
                String idiot = getPath(drl, e.getPath());
                if(idiot != null) {return idiot;}
            }
        }
        return null;
    }
    

提交回复
热议问题