Package jahuwaldt.io

Class ExtFilenameFilter

  • All Implemented Interfaces:
    java.io.FileFilter, java.io.FilenameFilter

    public class ExtFilenameFilter
    extends java.lang.Object
    implements java.io.FilenameFilter, java.io.FileFilter
    A convenience implementation of FilenameFilter and FileFilter that filters out all files except for those type extensions that it knows about. Extensions are of the type ".foo", which is typically found on Windows and Unix boxes, but not on the Macintosh prior to OS X. Case is ignored.

    Modified by: Joseph A. Huwaldt

    Version:
    September 16, 2016
    Author:
    Joseph A. Huwaldt, Date: June 18, 2004
    • Constructor Summary

      Constructors 
      Constructor Description
      ExtFilenameFilter()
      Creates a filename filter.
      ExtFilenameFilter​(java.lang.String extension)
      Creates a filename filter that accepts files with the given extension.
      ExtFilenameFilter​(java.lang.String... filters)
      Creates a file filter from the given string array.
      ExtFilenameFilter​(java.lang.String[] filters, java.lang.String description)
      Creates a file filter from the given string array and description.
      ExtFilenameFilter​(java.lang.String extension, java.lang.String description)
      Creates a file filter that accepts the given file type.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean accept​(java.io.File f)
      Return true if this file should be shown , false if it should not.
      boolean accept​(java.io.File dir, java.lang.String name)
      Return true if this file should be included in a file list, false if it shouldn't.
      void addExtension​(java.lang.String extension)
      Adds a file name "dot" extension to filter against.
      void addExtensions​(java.lang.String extensionList)
      Adds a list of extensions parsed from a comma, space or tab delimited list.
      void addFilename​(java.lang.String fileName)
      Adds a full filename to filter against.
      java.lang.String getDescription()
      Returns the human readable description of this filter.
      static java.lang.String getExtension​(java.io.File f)
      Return the extension portion of the file's name.
      static java.lang.String getExtension​(java.lang.String name)
      Return the extension portion of the file's name.
      boolean isExtensionListInDescription()
      Returns whether the extension list (.jpg,.gif, etc) should show up in the human readable description.
      void setDescription​(java.lang.String description)
      Sets the human readable description of this filter.
      void setExtensionListInDescription​(boolean useExtInDescription)
      Determines whether the extension list (.jpg,.gif, etc) should show up in the human readable description.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExtFilenameFilter

        public ExtFilenameFilter​(java.lang.String extension)
        Creates a filename filter that accepts files with the given extension. Example: new ExtFilenameFilter("jpg");
        Parameters:
        extension - The file name extension to use for this filter. Null extensions are ignored.
        See Also:
        addExtension(java.lang.String)
      • ExtFilenameFilter

        public ExtFilenameFilter​(java.lang.String extension,
                                 java.lang.String description)
        Creates a file filter that accepts the given file type. Example: new ExtFilenameFilter("jpg", "JPEG Image Images");

        Note that the "." before the extension is not needed, but it is fine if it is there.

        Parameters:
        extension - The file name extension to use for this filter. Null extensions are ignored.
        description - A description of the file type of this filter. Null is fine.
        See Also:
        addExtension(java.lang.String)
      • ExtFilenameFilter

        public ExtFilenameFilter​(java.lang.String... filters)
        Creates a file filter from the given string array. Example: new ExtFilenameFilter(String {"gif", "jpg"});

        Note that the "." before the extension is not needed, but it is fine if it is there.

        Parameters:
        filters - An array of String objects where each entry is a file name extension to be included in this filter. May not be null.
        See Also:
        addExtension(java.lang.String)
      • ExtFilenameFilter

        public ExtFilenameFilter​(java.lang.String[] filters,
                                 java.lang.String description)
        Creates a file filter from the given string array and description. Example: new ExtFilenameFilter(String {"gif", "jpg"}, "Gif and JPG Images");

        Note that the "." before the extension is not needed, but it is fine if it is there.

        Parameters:
        filters - An array of String objects where each entry is a file name extension to be included in this filter. Any null members of the array are ignored. The array itself may not be null.
        description - The description of the extensions in this filter set. Null is fine.
        See Also:
        addExtension(java.lang.String)
    • Method Detail

      • accept

        public boolean accept​(java.io.File f)
        Return true if this file should be shown , false if it should not.
        Specified by:
        accept in interface java.io.FileFilter
        Parameters:
        f - The file that is to be tested for compatibility with this filter.
      • accept

        public boolean accept​(java.io.File dir,
                              java.lang.String name)
        Return true if this file should be included in a file list, false if it shouldn't.
        Specified by:
        accept in interface java.io.FilenameFilter
        Parameters:
        dir - The directory in which the file was found.
        name - The name of the file.
      • getExtension

        public static java.lang.String getExtension​(java.lang.String name)
        Return the extension portion of the file's name. The extension will always be returned in lower case and without the ".".
        Parameters:
        name - The file name for which the extension is to be returned. May not be null.
        Returns:
        The extension portion of the file's name without the "." or "" if there is no extension.
        See Also:
        getExtension(java.io.File)
      • getExtension

        public static java.lang.String getExtension​(java.io.File f)
        Return the extension portion of the file's name. The extension will always be returned in lower case and without the ".".
        Parameters:
        f - The file object for which the extension is to be returned. May not be null.
        Returns:
        The extension portion of the file's name without the "." or "" if there is no extension.
        See Also:
        getExtension(java.lang.String)
      • addExtension

        public final void addExtension​(java.lang.String extension)
        Adds a file name "dot" extension to filter against.

        For example: the following code will create a filter that filters out all files except those that end in ".jpg" and ".tif":

         ExtFilenameFilter filter = new ExtFilenameFilter(); filter.addExtension("jpg");
         filter.addExtension("tif");
         

        Note that the "." before the extension is not needed, but it is fine if it is there.

        Parameters:
        extension - The file name extension to be added to this filter. May not be null.
      • addFilename

        public void addFilename​(java.lang.String fileName)
        Adds a full filename to filter against.

        For example: the following code will create a filter that filters out all files except those that end in ".jpg" and ".tif" or have the name "foo.bar":

         ExtFilenameFilter filter = new ExtFilenameFilter();
         filter.addExtension("jpg");
         filter.addExtension("tif");
         filter.addFileName("foo.bar");
         

        Parameters:
        fileName - A full file name to add to this filter for filtering against. May not be null.
      • addExtensions

        public void addExtensions​(java.lang.String extensionList)
        Adds a list of extensions parsed from a comma, space or tab delimited list.

        For example, the following will create a filter that filters out all files except those that end in ".jpg" and ".png":

            ExtFilenameFilter filter = new ExtFilenameFilter();
            filter.addExtensions("jpg,png,gif");
         

        Parameters:
        extensionList - A delimited list of extensions to add to the filter. May not be null.