org.apache.commons.io.comparator.CompositeFileComparator Example
Apache Commons IO is a library of utilities to assist with developing IO functionality. org.apache.commons.io.comparator
package provides various Comparator
implementations for File
s. CompositeFileComparator
is one of the classes.
This class can be used to sort lists or arrays of files (File
objects) by combining a number of other comparators defined within the same package (DirectoryFileComparator
, ExtensionFileComparator
, LastModifiedFileComparator
, NameFileComparator
, PathFileComparator
, SizeFileComparator
). This article shows usage of this class.
The class is from Apache Commons IO 2.4. Note the commons-io-2.4.jar
file is required in the classpath to compile and run the example. This library can be downloaded from the Apache Commons website.
The example is tested on Windows operating system. The Apache Commons IO 2.4 API usage requires Java SE 6 or greater.
1. An Example
This example sorts files in a directory by their type (or extension) and size (or length).
The composite comparator uses two delegate comparators to do the sorting. They are ExtensionFileComparator
and SizeFileComparator
. ExtensionFileComparator
compares by file name extensions for ordering. SizeFileComparator
compares file length for ordering.
The example program takes a directory with files as input File
objects. These file objects are copied to a File
array. Then the array is sorted using the CompositeFileComparator
class’s sort()
method.
Note that a real directory and files are required to run the example.
2. The Code
Example.java
import org.apache.commons.io.comparator.CompositeFileComparator; import org.apache.commons.io.comparator.ExtensionFileComparator; import org.apache.commons.io.comparator.SizeFileComparator; import org.apache.commons.io.FilenameUtils; import java.io.File; public class Example { public static void main(String [] args) { CompositeFileComparator comparator = new CompositeFileComparator( ExtensionFileComparator.EXTENSION_COMPARATOR, SizeFileComparator.SIZE_COMPARATOR); System.out.println("### Input files ###"); File dir = new File("X:\\testdir\\"); File [] files = dir.listFiles(); printArrayContents(files); System.out.println(""); System.out.println("### Array sorted (by extension and size) ###"); files = comparator.sort(files); printArrayContents(files); } private static void printArrayContents(File [] files) { for (File file : files) { String fileExtn = FilenameUtils.getExtension(file.getName()); System.out.println(file.getName()); System.out.println(" " + fileExtn + " " + file.length()); } } }
3. The Output
### Input files ### anecdotes.txt txt 29327 BoxingExample.java java 13678 Copy of sql scripts.txt txt 35200 FilenameUtilsExample1.java java 27360 FilenameUtilsExample2.java java 6838 notes.txt txt 8795 ### Array sorted (by extension and size) ### FilenameUtilsExample2.java java 6838 BoxingExample.java java 13678 FilenameUtilsExample1.java java 27360 notes.txt txt 8795 anecdotes.txt txt 29327 Copy of sql scripts.txt txt 35200
From the output:
- Note that the input files are ordered by their names and there are two types of files (
java
andtxt
). The terminal output also shows the file type and the file size. - After the sort, the files are ordered by the extension
java
andtxt
. Note the Java files are ordered by the size (6838, 13678, 27360).
4. Download Java Source Code
This was an example of org.apache.commons.io.comparator.CompositeFileComparator
You can download the full source code of this example here: Example.zip