Class WildcardFileFilter

  • All Implemented Interfaces:
    FileFilter, FilenameFilter, Serializable, FileVisitor<Path>, PathFilter, PathVisitor, IOFileFilter

    public class WildcardFileFilter
    extends AbstractFileFilter
    implements Serializable
    Filters files using the supplied wildcards.

    This filter selects files and directories based on one or more wildcards. Testing is case-sensitive by default, but this can be configured.

    The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines. The check is case-sensitive by default. See FilenameUtils.wildcardMatchOnSystem(String,String) for more information.

    For example:

    Using Classic IO

     File dir = new File(".");
     FileFilter fileFilter = new WildcardFileFilter("*test*.java~*~");
     File[] files = dir.listFiles(fileFilter);
     for (String file : files) {
         System.out.println(file);
     }
     

    Using NIO

     final Path dir = Paths.get("");
     final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new WildcardFileFilter("*test*.java~*~"));
     //
     // Walk one dir
     Files.walkFileTree(dir, Collections.emptySet(), 1, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getFileList());
     //
     visitor.getPathCounters().reset();
     //
     // Walk dir tree
     Files.walkFileTree(dir, visitor);
     System.out.println(visitor.getPathCounters());
     System.out.println(visitor.getDirList());
     System.out.println(visitor.getFileList());
     
    Since:
    1.3
    See Also:
    Serialized Form
    • Constructor Detail

      • WildcardFileFilter

        public WildcardFileFilter​(List<String> wildcards)
        Construct a new case-sensitive wildcard filter for a list of wildcards.
        Parameters:
        wildcards - the list of wildcards to match, not null
        Throws:
        IllegalArgumentException - if the pattern list is null
        ClassCastException - if the list does not contain Strings
      • WildcardFileFilter

        public WildcardFileFilter​(List<String> wildcards,
                                  IOCase caseSensitivity)
        Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
        Parameters:
        wildcards - the list of wildcards to match, not null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the pattern list is null
        ClassCastException - if the list does not contain Strings
      • WildcardFileFilter

        public WildcardFileFilter​(String wildcard)
        Construct a new case-sensitive wildcard filter for a single wildcard.
        Parameters:
        wildcard - the wildcard to match
        Throws:
        IllegalArgumentException - if the pattern is null
      • WildcardFileFilter

        public WildcardFileFilter​(String... wildcards)
        Construct a new case-sensitive wildcard filter for an array of wildcards.

        Parameters:
        wildcards - the array of wildcards to match
        Throws:
        IllegalArgumentException - if the pattern array is null
      • WildcardFileFilter

        public WildcardFileFilter​(String wildcard,
                                  IOCase caseSensitivity)
        Construct a new wildcard filter for a single wildcard specifying case-sensitivity.
        Parameters:
        wildcard - the wildcard to match, not null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the pattern is null
      • WildcardFileFilter

        public WildcardFileFilter​(String[] wildcards,
                                  IOCase caseSensitivity)
        Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.

        Parameters:
        wildcards - the array of wildcards to match, not null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the pattern array is null
    • Method Detail

      • accept

        public boolean accept​(File file)
        Checks to see if the file name matches one of the wildcards.
        Specified by:
        accept in interface FileFilter
        Specified by:
        accept in interface IOFileFilter
        Overrides:
        accept in class AbstractFileFilter
        Parameters:
        file - the file to check
        Returns:
        true if the file name matches one of the wildcards
      • accept

        public boolean accept​(File dir,
                              String name)
        Checks to see if the file name matches one of the wildcards.
        Specified by:
        accept in interface FilenameFilter
        Specified by:
        accept in interface IOFileFilter
        Overrides:
        accept in class AbstractFileFilter
        Parameters:
        dir - the file directory (ignored)
        name - the file name
        Returns:
        true if the file name matches one of the wildcards
      • accept

        public FileVisitResult accept​(Path file,
                                      BasicFileAttributes attributes)
        Checks to see if the file name matches one of the wildcards.
        Specified by:
        accept in interface IOFileFilter
        Specified by:
        accept in interface PathFilter
        Parameters:
        file - the file to check
        attributes - the file's basic attributes (TODO may be null).
        Returns:
        true if the file name matches one of the wildcards.
        Since:
        2.9.0
      • toString

        public String toString()
        Provide a String representation of this file filter.
        Overrides:
        toString in class AbstractFileFilter
        Returns:
        a String representation