Home » Core Java » apache » commons » io » comparator » PathFileComparator » org.apache.commons.io.comparator.PathFileComparator Example

About Prasad Saya

Avatar photo
Prasad Saya is a software engineer with over ten years’ experience in application development, maintenance, testing and consulting on various platforms. He is a certified Java and Java EE developer. At present his interest is in developing Java applications. He also has experience working with databases and ERP applications.

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 Files. 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 File objects. These file objects are copied to a 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


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();

        System.out.println("### Array sorted ###");
        files = comparator.sort(files);

    private static void printArrayContents(File [] files) {

        for (File file : files) {

3. The Output

### Input files ###
X:\testdir\test1\copy of sql scripts.txt

### Array sorted ###
X:\testdir\test1\copy of sql scripts.txt

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

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!


1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design


and many more ....


Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions


Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments