org.apache.commons.io.filefilter

## Class AgeFileFilter

- java.lang.Object
- - org.apache.commons.io.filefilter.AbstractFileFilter
  - - org.apache.commons.io.filefilter.AgeFileFilter

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

      public class AgeFileFilter
      extends AbstractFileFilter
      implements Serializable

  Filters files based on a cutoff time, can filter either newer files or files equal to or older.

  For example, to print all files and directories in the current directory older than one day:

  ## Using Classic IO

       Path dir = Paths.get("");
       // We are interested in files older than one day
       long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
       String[] files = dir.list(new AgeFileFilter(cutoff));
       for (String file : files) {
           System.out.println(file);
       }

  ## Using NIO

       Path dir = Paths.get("");
       // We are interested in files older than one day
       long cutoff = System.currentTimeMillis() - (24 * 60 * 60 * 1000);
       AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new AgeFileFilter(cutoff));
       //
       // 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.2
  See Also:
    `FileFilterUtils.ageFileFilter(Date)`, `FileFilterUtils.ageFileFilter(File)`, `FileFilterUtils.ageFileFilter(long)`, `FileFilterUtils.ageFileFilter(Date, boolean)`, `FileFilterUtils.ageFileFilter(File, boolean)`, `FileFilterUtils.ageFileFilter(long, boolean)`, Serialized Form

- - ### Field Summary

    - ### Fields inherited from interface org.apache.commons.io.filefilter.IOFileFilter

      `EMPTY_STRING_ARRAY`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `AgeFileFilter(Date cutoffDate)` Constructs a new age file filter for files older than (at or before) a certain cutoff date.
    `AgeFileFilter(Date cutoffDate, boolean acceptOlder)` Constructs a new age file filter for files on any one side of a certain cutoff date.
    `AgeFileFilter(File cutoffReference)` Constructs a new age file filter for files older than (at or before) a certain File (whose last modification time will be used as reference).
    `AgeFileFilter(File cutoffReference, boolean acceptOlder)` Constructs a new age file filter for files on any one side of a certain File (whose last modification time will be used as reference).
    `AgeFileFilter(long cutoffMillis)` Constructs a new age file filter for files equal to or older than a certain cutoff
    `AgeFileFilter(long cutoffMillis, boolean acceptOlder)` Constructs a new age file filter for files on any one side of a certain cutoff.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `boolean` | `accept(File file)` Checks to see if the last modification of the file matches cutoff favorably.
    `FileVisitResult` | `accept(Path file, BasicFileAttributes attributes)` Checks to see if the last modification of the file matches cutoff favorably.
    `String` | `toString()` Provide a String representation of this file filter.

    - ### Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilter

      `accept, handle, postVisitDirectory, preVisitDirectory, visitFile, visitFileFailed`

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

    - ### Methods inherited from interface org.apache.commons.io.filefilter.IOFileFilter

      `and, negate, or`

- - ### Constructor Detail

    - #### AgeFileFilter

          public AgeFileFilter(Date cutoffDate)

      Constructs a new age file filter for files older than (at or before) a certain cutoff date.
      Parameters:
        `cutoffDate` - the threshold age of the files

    - #### AgeFileFilter

          public AgeFileFilter(Date cutoffDate,
                               boolean acceptOlder)

      Constructs a new age file filter for files on any one side of a certain cutoff date.
      Parameters:
        `cutoffDate` - the threshold age of the files
        `acceptOlder` - if true, older files (at or before the cutoff) are accepted, else newer ones (after the cutoff).

    - #### AgeFileFilter

          public AgeFileFilter(File cutoffReference)

      Constructs a new age file filter for files older than (at or before) a certain File (whose last modification time will be used as reference).
      Parameters:
        `cutoffReference` - the file whose last modification time is used as the threshold age of the files

    - #### AgeFileFilter

          public AgeFileFilter(File cutoffReference,
                               boolean acceptOlder)

      Constructs a new age file filter for files on any one side of a certain File (whose last modification time will be used as reference).
      Parameters:
        `cutoffReference` - the file whose last modification time is used as the threshold age of the files
        `acceptOlder` - if true, older files (at or before the cutoff) are accepted, else newer ones (after the cutoff).

    - #### AgeFileFilter

          public AgeFileFilter(long cutoffMillis)

      Constructs a new age file filter for files equal to or older than a certain cutoff
      Parameters:
        `cutoffMillis` - The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).

    - #### AgeFileFilter

          public AgeFileFilter(long cutoffMillis,
                               boolean acceptOlder)

      Constructs a new age file filter for files on any one side of a certain cutoff.
      Parameters:
        `cutoffMillis` - The cutoff time threshold measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970).
        `acceptOlder` - if true, older files (at or before the cutoff) are accepted, else newer ones (after the cutoff).

  - ### Method Detail

    - #### accept

          public boolean accept(File file)

      Checks to see if the last modification of the file matches cutoff favorably.

      If last modification time equals cutoff and newer files are required, file IS NOT selected. If last modification time equals cutoff and older files are required, file IS selected.

      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

    - #### accept

          public FileVisitResult accept(Path file,
                                        BasicFileAttributes attributes)

      Checks to see if the last modification of the file matches cutoff favorably.

      If last modification time equals cutoff and newer files are required, file IS NOT selected. If last modification time equals cutoff and older files are required, file IS selected.

      Specified by:
        `accept` in interface `PathFilter`
      Specified by:
        `accept` in interface `IOFileFilter`
      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

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