org.apache.commons.io.filefilter

Class EmptyFileFilter

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

    public class EmptyFileFilter
    extends AbstractFileFilter
    implements Serializable
    This filter accepts files or directories that are empty.

    If the File is a directory it checks that it contains no files.

    Example, showing how to print out a list of the current directory's empty files/directories:

    Using Classic IO

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

    Example, showing how to print out a list of the current directory's non-empty files/directories:

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

    Using NIO

     final Path dir = Paths.get("");
     final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(EmptyFileFilter.EMPTY);
     //
     // 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
    • Field Detail

      • EMPTY

        public static final IOFileFilter EMPTY
        Singleton instance of empty filter
      • NOT_EMPTY

        public static final IOFileFilter NOT_EMPTY
        Singleton instance of not-empty filter
    • Constructor Detail

      • EmptyFileFilter

        protected EmptyFileFilter()
        Restrictive constructor.
    • Method Detail

      • accept

        public boolean accept(File file)
        Checks to see if the file is empty.
        Specified by:
        accept in interface  FileFilter
        Specified by:
        accept in interface  IOFileFilter
        Overrides:
        accept in class  AbstractFileFilter
        Parameters:
        file - the file or directory to check
        Returns:
        true if the file or directory is empty, otherwise false.
      • accept

        public FileVisitResult accept(Path file,
                                      BasicFileAttributes attributes)
        Checks to see if the file is empty.
        Specified by:
        accept in interface  PathFilter
        Specified by:
        accept in interface  IOFileFilter
        Parameters:
        file - the file or directory to check
        attributes - the file's basic attributes (TODO may be null).
        Returns:
        true if the file or directory is empty, otherwise false.
        Since:
        2.9.0

Copyright © 2002–2021 The Apache Software Foundation. All rights reserved.