public class SizeFileFilter extends AbstractFileFilter implements java.io.Serializable
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 (String file : files) { System.out.println(file); }
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());
Modifier and Type | Field and Description |
---|---|
private boolean |
acceptLarger
Whether the files accepted will be larger or smaller.
|
private static long |
serialVersionUID |
private long |
size
The size threshold.
|
EMPTY_STRING_ARRAY
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(java.io.File file)
Checks to see if the size of the file is favorable.
|
private boolean |
accept(long length) |
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.
|
java.lang.String |
toString()
Provide a String representation of this file filter.
|
java.nio.file.FileVisitResult |
visitFile(java.nio.file.Path file,
java.nio.file.attribute.BasicFileAttributes attrs) |
accept, handle, postVisitDirectory, preVisitDirectory, toFileVisitResult, visitFileFailed
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
and, negate, or
private static final long serialVersionUID
private final boolean acceptLarger
private final long size
public SizeFileFilter(long size)
size
- the threshold size of the filesjava.lang.IllegalArgumentException
- if the size is negativepublic SizeFileFilter(long size, boolean acceptLarger)
size
- the threshold size of the filesacceptLarger
- if true, files equal to or larger are accepted,
otherwise smaller ones (but not equal to)java.lang.IllegalArgumentException
- if the size is negativepublic boolean accept(java.io.File file)
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.
accept
in interface java.io.FileFilter
accept
in interface IOFileFilter
accept
in class AbstractFileFilter
file
- the File to checkprivate boolean accept(long length)
public java.nio.file.FileVisitResult accept(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attributes)
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.
accept
in interface PathFilter
accept
in interface IOFileFilter
file
- the File to checkattributes
- the file's basic attributes (TODO may be null).public java.lang.String toString()
toString
in class AbstractFileFilter
public java.nio.file.FileVisitResult visitFile(java.nio.file.Path file, java.nio.file.attribute.BasicFileAttributes attrs) throws java.io.IOException
visitFile
in interface java.nio.file.FileVisitor<java.nio.file.Path>
visitFile
in class AbstractFileFilter
java.io.IOException