Find files in a folder using Java

前端 未结 12 1918
日久生厌
日久生厌 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条回答
  •  [愿得一人]
    2020-11-28 08:20

    To elaborate on this response, Apache IO Utils might save you some time. Consider the following example that will recursively search for a file of a given name:

        File file = FileUtils.listFiles(new File("the/desired/root/path"), 
                    new NameFileFilter("filename.ext"), 
                    FileFilterUtils.trueFileFilter()
                ).iterator().next();
    

    See:

    • org.apache.commons.io.FileUtils
    • org.apache.commons.io.filefilter.FileFilterUtils
    • https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/filefilter/IOFileFilter.html

提交回复
热议问题