Comparator implementations
for Files.See: Description
| Class | Description |
|---|---|
| CompositeFileComparator |
Compare two files using a set of delegate file
Comparator. |
| DefaultFileComparator |
Compare two files using the default
File.compareTo(File) method. |
| DirectoryFileComparator |
Compare two files using the
File.isDirectory() method. |
| ExtensionFileComparator |
Compare the file name extensions for order
(see
FilenameUtils.getExtension(String)). |
| LastModifiedFileComparator |
Compare the last modified date/time of two files for order
(see
File.lastModified()). |
| NameFileComparator |
Compare the names of two files for order (see
File.getName()). |
| PathFileComparator |
Compare the path of two files for order (see
File.getPath()). |
| SizeFileComparator |
Compare the length/size of two files for order (see
File.length() and FileUtils.sizeOfDirectory(File)). |
This package provides various Comparator implementations
for Files.
All the compartors include convenience utility sort(File...) and
sort(List) methods.
For example, to sort the files in a directory by name:
File[] files = dir.listFiles();
NameFileComparator.NAME_COMPARATOR.sort(files);
...alternatively you can do this in one line:
File[] files = NameFileComparator.NAME_COMPARATOR.sort(dir.listFiles());
The CompositeFileComparator can be used to compare (and sort lists or arrays of files) by combining a number other comparators.
For example, to sort an array of files by type (i.e. directory or file) and then by name:
CompositeFileComparator comparator =
new CompositeFileComparator(
DirectoryFileComparator.DIRECTORY_COMPARATOR,
NameFileComparator.NAME_COMPARATOR);
File[] files = dir.listFiles();
comparator.sort(files);
The Comparator implementations have some convenience
singleton(thread-safe) instances ready to use:
File.compareTo(File) method.
File.compareTo(File) method.
File.isDirectory() method (directories < files).
File.isDirectory() method (directories >files).
FilenameUtils.getExtension(String) method.
FilenameUtils.getExtension(String) method.
FilenameUtils.getExtension(String) method.
FilenameUtils.getExtension(String) method.
FilenameUtils.getExtension(String) method.
FilenameUtils.getExtension(String) method.
File.lastModified() method.
File.lastModified() method.
File.getName() method.
File.getName() method.
File.getName() method.
File.getName() method.
File.getName() method.
File.getName() method.
File.getPath() method.
File.getPath() method.
File.getPath() method.
File.getPath() method.
File.getPath() method.
File.getPath() method.
File.length() method (directories treated as zero length).
File.length() method (directories treated as zero length).
FileUtils.sizeOfDirectory(File) method
(sums the size of a directory's contents).
FileUtils.sizeOfDirectory(File) method
(sums the size of a directory's contents).
Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.