A Java FileFilter example for image files

If you need some source code for a Java FileFilter for image files, this code can get you started:

class ImageFileFilter implements FileFilter {
    private final String[] okFileExtensions = new String[] { "jpg", "jpeg", "png", "gif" };

    public boolean accept(File file) {
        for (String extension : okFileExtensions) {
            if (file.getName().toLowerCase().endsWith(extension)) {
                return true;
            }
        }
        return false;
    }
}

You can use that file filter with the Java File class like this:

File dir = new File(canonDirectoryName);
File[] filesInDir = dir.listFiles(new ImageFileFilter());
if (filesInDir.length == 0) return;
// your custom code here ...

As mentioned, if you need some code for a Java FileFilter class that works with image files, I hope this is helpful.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.