org.apache.commons.io.comparator.PathFileComparator 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. PathFileComparator
is one of the classes.
This class can be used to sort lists or arrays of files (File
objects) by their path. The path value is a String
returned from the File
class’s getPath()
method. The sort can be done in a case-sensitive, case-insensitive or system dependent case sensitive way. 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.
Note on String Comparison in Java:
The File
class’s getPath()
method returns a String
representation of the abstract pathname. This string value is used in sorting. The String
class implements Comparable
interface. This allows String
objects in an array or a collection be sorted by their natural ordering (special characters, numbers, upper case, lower case). For example take the strings “apple”, “Dell”, “HP” and “lenovo”. When these strings are sorted, they are in this order: “Dell”, “HP”, “apple”, “lenovo”.
1. An Example
This example sorts files in a directory by using the PathFileComparator
‘s sort()
method.
The example program takes a directory with files as input
objects. These file objects are copied to a File
File
array. Then the array is sorted using the PathFileComparator
class’s sort()
method.
Note that a real directory and files are required to run the example.
2. The Code
PathFileComparatorExample.java
import org.apache.commons.io.comparator.PathFileComparator; import java.io.File; public class PathFileComparatorExample { public static void main(String [] args) { PathFileComparator comparator = new PathFileComparator(); 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 ###"); files = comparator.sort(files); printArrayContents(files); } private static void printArrayContents(File [] files) { for (File file : files) { System.out.println(file.getPath()); } } }
3. The Output
### Input files ### X:\testdir\test1\anecdotes.txt X:\testdir\test1\BoxingExample.java X:\testdir\test1\copy of sql scripts.txt X:\testdir\test1\FilenameUtilsExample.java X:\testdir\test1\notes.txt X:\testdir\test1\screenshots.docx X:\testdir\test1\WatchEventExample.java ### Array sorted ### X:\testdir\test1\BoxingExample.java X:\testdir\test1\FilenameUtilsExample.java X:\testdir\test1\WatchEventExample.java X:\testdir\test1\anecdotes.txt X:\testdir\test1\copy of sql scripts.txt X:\testdir\test1\notes.txt X:\testdir\test1\screenshots.docx
From the output:
- Note that the input files are ordered by their file names. After the sort the output shows the file names ordered with upper case first and then the lower case.
4. Download Java Source Code
This was an example of org.apache.commons.io.comparator.PathFileComparator
You can download the full source code of this example here: PathFileComparatorExample.zip