Use of FilenameFilter

后端 未结 5 1706
半阙折子戏
半阙折子戏 2020-12-15 05:33

I have a directory:

File dir = new File(MY_PATH);

I would like to list all the files whose name is indicated as integer numbers strings, e.

相关标签:
5条回答
  • 2020-12-15 05:52

    I do it as:

        File folder = new File(".");
        File[] listOfFiles = folder.listFiles();
        for (File file : listOfFiles) {
            if (file.isFile()) {
                if (file.toString().endsWith(".sql")) {
                    System.out.println(file.getName());
                }
            }
        }
        System.out.println("End!!");
    
    0 讨论(0)
  • 2020-12-15 05:59

    Since Java 8, you can simply use a lambda expression to specify your custom filter:

    dir.list((dir1, name) -> name.equals("foo"));
    

    In the above example, only files with the name "foo" will make it through. Use your own logic of course.

    0 讨论(0)
  • 2020-12-15 06:00

    You should override accept in the interface FilenameFilter and make sure that the parameter name has only numeric chars. You can check this by using matches:

    String[] list = dir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.matches("[0-9]+");
        }
    });
    
    0 讨论(0)
  • 2020-12-15 06:02

    preferably as an instance of an anonymous inner class passsed as parameter to File#list.

    for example, to list only files ending with the extension .txt:

    File dir = new File("/home");
    String[] list = dir.list(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            return name.toLowerCase().endsWith(".txt");
        }
    });
    

    To list only files whose filenames are integers of exactly 2 digits you can use the following in the accept method:

    return name.matches("\\d{2}");
    

    for one or more digits:

    return name.matches("\\d+");    
    

    EDIT (as response to @crashprophet's comment)

    Pass a set of extensions of files to list

    class ExtensionAwareFilenameFilter implements FilenameFilter {
    
        private final Set<String> extensions;
    
        public ExtensionAwareFilenameFilter(String... extensions) {
            this.extensions = extensions == null ? 
                Collections.emptySet() : 
                    Arrays.stream(extensions)
                        .map(e -> e.toLowerCase()).collect(Collectors.toSet());
        }
    
        @Override
        public boolean accept(File dir, String name) {
            return extensions.isEmpty() || 
                extensions.contains(getFileExtension(name));
        }
    
        private String getFileExtension(String filename) {
            String ext = null;
            int i = filename .lastIndexOf('.');
            if(i != -1 && i < filename .length()) {
                ext = filename.substring(i+1).toLowerCase();
            }
            return ext;
        }
    }
    
    
    @Test
    public void filefilter() {
        Arrays.stream(new File("D:\\downloads").
            list(new ExtensionAwareFilenameFilter("pdf", "txt")))
                .forEach(e -> System.out.println(e));
    }
    
    0 讨论(0)
  • 2020-12-15 06:07

    Here's the what I wound up with. It uses a nice lambda expression that can be easily twisted to your own designs...

    File folder = new File(FullPath);
    String[] files = folder.list((lamFolder, lamName) -> lamName.matches("[0-9]+"));
    if(files == null) {
        System.out.println("Stuff wrongly: no matching files found.");
    } else {
        for(String file : files) {
            System.out.println("HOORAY: I found this "+ file);
        }
    }
    
    0 讨论(0)
提交回复
热议问题