Package org.apache.commons.io.filefilter
Class SizeFileFilter
- java.lang.Object
-
- org.apache.commons.io.filefilter.AbstractFileFilter
-
- org.apache.commons.io.filefilter.SizeFileFilter
-
- All Implemented Interfaces:
java.io.FileFilter,java.io.FilenameFilter,java.io.Serializable,java.nio.file.FileVisitor<java.nio.file.Path>,PathFilter,PathVisitor,IOFileFilter
public class SizeFileFilter extends AbstractFileFilter implements java.io.Serializable
Filters files based on size, can filter either smaller files or files equal to or larger than a given threshold.For example, to print all files and directories in the current directory whose size is greater than 1 MB:
Using Classic IO
File dir = new File("."); String[] files = dir.list(new SizeFileFilter(1024 * 1024)); for (String file : files) { System.out.println(file); }Using NIO
final Path dir = Paths.get(""); final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new SizeFileFilter(1024 * 1024)); // // 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());
-
-
Field Summary
-
Fields inherited from interface org.apache.commons.io.filefilter.IOFileFilter
EMPTY_STRING_ARRAY
-
-
Constructor Summary
Constructors Constructor Description SizeFileFilter(long size)Constructs a new size file filter for files equal to or larger than a certain size.SizeFileFilter(long size, boolean acceptLarger)Constructs a new size file filter for files based on a certain size threshold.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccept(java.io.File file)Checks to see if the size of the file is favorable.java.nio.file.FileVisitResultaccept(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attributes)Checks to see if the size of the file is favorable.java.lang.StringtoString()Provide a String representation of this file filter.java.nio.file.FileVisitResultvisitFile(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attrs)-
Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilter
accept, postVisitDirectory, preVisitDirectory, visitFileFailed
-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.io.filefilter.IOFileFilter
and, negate, or
-
-
-
-
Constructor Detail
-
SizeFileFilter
public SizeFileFilter(long size)
Constructs a new size file filter for files equal to or larger than a certain size.- Parameters:
size- the threshold size of the files- Throws:
java.lang.IllegalArgumentException- if the size is negative
-
SizeFileFilter
public SizeFileFilter(long size, boolean acceptLarger)Constructs a new size file filter for files based on a certain size threshold.- Parameters:
size- the threshold size of the filesacceptLarger- if true, files equal to or larger are accepted, otherwise smaller ones (but not equal to)- Throws:
java.lang.IllegalArgumentException- if the size is negative
-
-
Method Detail
-
accept
public boolean accept(java.io.File file)
Checks to see if the size of the file is favorable.If size equals threshold and smaller files are required, file IS NOT selected. If size equals threshold and larger files are required, file IS selected.
- Specified by:
acceptin interfacejava.io.FileFilter- Specified by:
acceptin interfaceIOFileFilter- Overrides:
acceptin classAbstractFileFilter- Parameters:
file- the File to check- Returns:
- true if the file name matches
-
accept
public java.nio.file.FileVisitResult accept(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attributes)Checks to see if the size of the file is favorable.If size equals threshold and smaller files are required, file IS NOT selected. If size equals threshold and larger files are required, file IS selected.
- Specified by:
acceptin interfaceIOFileFilter- Specified by:
acceptin interfacePathFilter- Parameters:
file- the File to checkattributes- the file's basic attributes (TODO may be null).- Returns:
- true if the file name matches
-
toString
public java.lang.String toString()
Provide a String representation of this file filter.- Overrides:
toStringin classAbstractFileFilter- Returns:
- a String representation
-
visitFile
public java.nio.file.FileVisitResult visitFile(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attrs) throws java.io.IOException- Specified by:
visitFilein interfacejava.nio.file.FileVisitor<java.nio.file.Path>- Overrides:
visitFilein classAbstractFileFilter- Throws:
java.io.IOException
-
-