Class NameFileFilter

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

    public class NameFileFilter
    extends AbstractFileFilter
    implements Serializable
    Filters file names for a certain name.

    For example, to print all files and directories in the current directory whose name is Test:

    Using Classic IO

     File dir = new File(".");
     String[] files = dir.list(new NameFileFilter("Test"));
     for (String file : files) {
         System.out.println(file);
     }
     

    Using NIO

     final Path dir = Paths.get("");
     final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new NameFileFilter("Test"));
     //
     // 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.0
    See Also:
    FileFilterUtils.nameFileFilter(String), FileFilterUtils.nameFileFilter(String, IOCase), Serialized Form
    • Constructor Detail

      • NameFileFilter

        public NameFileFilter​(List<String> names)
        Constructs a new case-sensitive name file filter for a list of names.
        Parameters:
        names - the names to allow, must not be null
        Throws:
        IllegalArgumentException - if the name list is null
        ClassCastException - if the list does not contain Strings
      • NameFileFilter

        public NameFileFilter​(List<String> names,
                              IOCase caseSensitivity)
        Constructs a new name file filter for a list of names specifying case-sensitivity.
        Parameters:
        names - the names to allow, must not be null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the name list is null
        ClassCastException - if the list does not contain Strings
      • NameFileFilter

        public NameFileFilter​(String name)
        Constructs a new case-sensitive name file filter for a single name.
        Parameters:
        name - the name to allow, must not be null
        Throws:
        IllegalArgumentException - if the name is null
      • NameFileFilter

        public NameFileFilter​(String... names)
        Constructs a new case-sensitive name file filter for an array of names.

        The array is not cloned, so could be changed after constructing the instance. This would be inadvisable however.

        Parameters:
        names - the names to allow, must not be null
        Throws:
        IllegalArgumentException - if the names array is null
      • NameFileFilter

        public NameFileFilter​(String name,
                              IOCase caseSensitivity)
        Construct a new name file filter specifying case-sensitivity.
        Parameters:
        name - the name to allow, must not be null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the name is null
      • NameFileFilter

        public NameFileFilter​(String[] names,
                              IOCase caseSensitivity)
        Constructs a new name file filter for an array of names specifying case-sensitivity.
        Parameters:
        names - the names to allow, must not be null
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        Throws:
        IllegalArgumentException - if the names array is null
    • Method Detail

      • accept

        public FileVisitResult accept​(Path file,
                                      BasicFileAttributes attributes)
        Checks to see if the file name matches.
        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
        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