FileDocCategorySizeDatePackage
SizeFileFilter.javaAPI DocAndroid 1.5 API3618Wed May 06 22:42:46 BST 2009org.apache.commons.io.filefilter

SizeFileFilter

public class SizeFileFilter extends AbstractFileFilter implements 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:

File dir = new File(".");
String[] files = dir.list( new SizeFileFilter(1024 * 1024) );
for ( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}
author
Rahul Akolkar
version
$Id: SizeFileFilter.java 591058 2007-11-01 15:47:05Z niallp $
since
Commons IO 1.2

Fields Summary
private final long
size
The size threshold.
private final boolean
acceptLarger
Whether the files accepted will be larger or smaller.
Constructors Summary
public SizeFileFilter(long size)
Constructs a new size file filter for files equal to or larger than a certain size.

param
size the threshold size of the files
throws
IllegalArgumentException if the size is negative

        this(size, true);
    
public SizeFileFilter(long size, boolean acceptLarger)
Constructs a new size file filter for files based on a certain size threshold.

param
size the threshold size of the files
param
acceptLarger if true, files equal to or larger are accepted, otherwise smaller ones (but not equal to)
throws
IllegalArgumentException if the size is negative

        if (size < 0) {
            throw new IllegalArgumentException("The size must be non-negative");
        }
        this.size = size;
        this.acceptLarger = acceptLarger;
    
Methods Summary
public booleanaccept(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.

param
file the File to check
return
true if the filename matches

        boolean smaller = file.length() < size;
        return acceptLarger ? !smaller : smaller;
    
public java.lang.StringtoString()
Provide a String representaion of this file filter.

return
a String representaion

        String condition = acceptLarger ? ">=" : "<";
        return super.toString() + "(" + condition + size + ")";