Find files in a folder using Java

前端 未结 12 1926
日久生厌
日久生厌 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:18

    For list out Json files from your given directory.

    import java.io.File;
        import java.io.FilenameFilter;
    
        public class ListOutFilesInDir {
            public static void main(String[] args) throws Exception {
    
                File[] fileList = getFileList("directory path");
    
                for(File file : fileList) {
                    System.out.println(file.getName());
                }
            }
    
            private static File[] getFileList(String dirPath) {
                File dir = new File(dirPath);   
    
                File[] fileList = dir.listFiles(new FilenameFilter() {
                    public boolean accept(File dir, String name) {
                        return name.endsWith(".json");
                    }
                });
                return fileList;
            }
        }
    

提交回复
热议问题