org.apache.commons.io.comparator.NameFileComparator 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. NameFileComparator
is one of the classes.
This class can be used to sort lists or arrays of files (File
objects) by their name. The File
class’s getName()
method is used for comparing two files; the method returns the name of the file as a String
. The sort can be done either in a case-sensitive, case-insensitive or system dependent case sensitive way.
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 examples. 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.
The article shows two examples using the NameFileComparator
class. The first example uses the sort()
method to sort an array of files, in case-sensitive way. The second example uses the field NAME_INSENSITIVE_REVERSE
as a comparator to perform a case-insensitive and reverse sort.
1. A Case-Sensitive Sort Example
This example sorts files in a directory by using the NameFileComparator
‘s sort()
method.
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 NameFileComparator
class’s sort()
method.
Note that a real directory and files are required to run the example.
1.1. The Code
NameFileComparatorExample1.java
import org.apache.commons.io.comparator.NameFileComparator; import java.io.File; public class NameFileComparatorExample1 { public static void main(String [] args) { NameFileComparator comparator = new NameFileComparator(); System.out.println("### Input files ###"); File dir = new File("X:\\testdir\\"); File [] files = dir.listFiles(); printArrayContents(files); 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.getName()); } System.out.println(""); } }
1.2. The Output
### Input files ### anecdotes.txt BoxingExample.java copy of sql scripts.txt FilenameUtilsExample.java notes.txt screenshots.docx WatchEventExample.java ### Array sorted ### BoxingExample.java FilenameUtilsExample.java WatchEventExample.java anecdotes.txt copy of sql scripts.txt notes.txt screenshots.docx
From the output:
- Input files: Note that the input files are listed by their names, in a case-insensitive way (a, B, c, F…).
- Array sorted: The output shows the sorted array of files in a case-sensitive way. Note that the upper case names are sorted first and then the lower case names.
2. A Case-Insensitive Reverse Sort Example
NameFileComparator
class defines six fields of type Comparator<File>
. These comparators can be used to sort or reverse sort arrays or collections of files in case-sensitive or case-insensitive manner.
This example uses the field NAME_INSENSITIVE_REVERSE
to reverse sort an array of files in a case-insensitive way.
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 Arrays
class’s sort()
static method. The method takes the array to be sorted and a comparator as parameters.
Note that a real directory and files are required to run the example.
2.1. The Code
NameFileComparatorExample2.java
import org.apache.commons.io.comparator.NameFileComparator; import java.io.File; import java.util.Arrays; public class NameFileComparatorExample2 { public static void main(String [] args) { System.out.println("### Input files ###"); File dir = new File("X:\\testdir\\"); File [] files = dir.listFiles(); printArrayContents(files); System.out.println("### Array reverse sorted ###"); Arrays.sort(files, NameFileComparator.NAME_INSENSITIVE_REVERSE); printArrayContents(files); } private static void printArrayContents(File [] files) { for (File file : files) { System.out.println(file.getName()); } System.out.println(""); } }
2.2. The Output
### Input files ### anecdotes.txt BoxingExample.java copy of sql scripts.txt FilenameUtilsExample.java notes.txt screenshots.docx WatchEventExample.java ### Array sorted ### WatchEventExample.java screenshots.docx notes.txt FilenameUtilsExample.java copy of sql scripts.txt BoxingExample.java anecdotes.txt
From the output:
- Input files: Note that the input files are listed by their names, in a case-insensitive way.
- Array sorted: The output shows the sorted array of files in a case-insensitive way, and in reverse order.
3. Download Java Source Code
This was an example of org.apache.commons.io.comparator.NameFileComparator
You can download the full source code of this example here: NameFileComparatorExample.zip