org.apache.commons.io.comparator.DefaultFileComparator 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 Files. DefaultFileComparator is one of the classes.
This class can be used to sort lists or arrays of files (File objects) by using the default file comparison. 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 shows the usage of the two sort methods. The methods take an array or a List collection of File objects, sort them and return the sorted File array or List respectively.
The program takes three directories with files as input File objects. These file objects are copied to an array as well as a List of type File. Then the array and list are sorted using the DefaultFileComparator class’s two sort methods respectively.
Note that real directories and files are required to run the example.
2. The Code
Example.java
import org.apache.commons.io.comparator.DefaultFileComparator;
import java.io.File;
import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
public class Example {
private static DefaultFileComparator dfc = new DefaultFileComparator();
public static void main(String [] args) {
System.out.println("Three input directories:");
File dir1 = new File("X:\\testdir\\src\\");
File [] ff1 = dir1.listFiles();
printArrayContents(ff1);
File dir2 = new File("X:\\testdir\\classes\\");
File [] ff2 = dir2.listFiles();
printArrayContents(ff2);
File dir3 = new File("X:\\testdir\\deploy\\");
File [] ff3 = dir3.listFiles();
printArrayContents(ff3);
// add all dir files to a list
List<File> fileList = new ArrayList<File>();
Collections.addAll(fileList, ff1);
Collections.addAll(fileList, ff2);
Collections.addAll(fileList, ff3);
// make a copy to an array
System.out.println("All files before sort:");
File [] fileArray = new File [fileList.size()];
int i = 0;
for (File file : fileList) {
System.out.println(file);
fileArray [i++] = file;
}
System.out.println("");
// sort the array
System.out.println("Array sorted:");
fileArray = dfc.sort(fileArray)
printArrayContents(fileArray);
// sort the list
System.out.println("List sorted:");
fileList = dfc.sort(fileList);
printListContents(fileList);
}
private static void printArrayContents(File [] files) {
for (File file : files) {
System.out.println(file);
}
System.out.println("");
}
private static void printListContents(List<File> files) {
for (File file : files) {
System.out.println(file);
}
}
}3. The Output
The three input directories: X:\testdir\src\a.java X:\testdir\src\b.java X:\testdir\classes\a.class X:\testdir\classes\b.class X:\testdir\deploy\a.class X:\testdir\deploy\b.class X:\testdir\deploy\install.txt All files before sort: X:\testdir\src\a.java X:\testdir\src\b.java X:\testdir\classes\a.class X:\testdir\classes\b.class X:\testdir\deploy\a.class X:\testdir\deploy\b.class X:\testdir\deploy\install.txt Array sorted: X:\testdir\classes\a.class X:\testdir\classes\b.class X:\testdir\deploy\a.class X:\testdir\deploy\b.class X:\testdir\deploy\install.txt X:\testdir\src\a.java X:\testdir\src\b.java List sorted: X:\testdir\classes\a.class X:\testdir\classes\b.class X:\testdir\deploy\a.class X:\testdir\deploy\b.class X:\testdir\deploy\install.txt X:\testdir\src\a.java X:\testdir\src\b.java
From the output:
- The three input directories: This shows the files in the three directories
src,classesanddeployin theX:\testdir\. - All files before sort: The three sets of
Fileobjects are copied to an array as well as aListof typeFile. - Array sorted: This has the
Fileobjects sorted. Note thesrc,classesanddeploydirectory order is changed after the sort. - List sorted: The sorting is similar to that of the array sorting.
4. Download Java Source Code
This was an example of org.apache.commons.io.comparator.DefaultFileComparator.
You can download the full source code of this example here: Example.zip
