Search code examples
javaiofilefilter

listFiles(FileFilter) instead of listFiles(FileNameFilter)


When I try to use the method someFile.listFiles(FileFilter foobar) I get the error

The method listFiles(FilenameFilter) in the type File is not applicable for the arguments (FileFilter)

While the javadoc of File clearly contains: public File[] listFiles(FileFilter filter) (http://docs.oracle.com/javase/7/docs/api/java/io/File.html)

How do I solve this?

PS: My code is:

    FileFilter folderFilter = new FileFilter() {
        public boolean accept(File file) {
            return file.isDirectory();
        }
        @Override
        public String getDescription() {
            return "some filter";
        }
    };
    File[] foobars = someFile.listFiles(folderFilter);

Solution

  • Looks like you've imported

    javax.swing.filechooser.FileFilter
    

    instead of

    java.io.FileFilter
    

    Probably occurred when selecting the import from the IDE

    If the correct import is used, the code should not compile due to the existence of the getDescription method which is only found in the former. The description is used for display purposes on JFileChooser dialogs and doesnt apply here.