DirectoryFileComparator

org.apache.commons.io.comparator.DirectoryFileComparator 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. DirectoryFileComparator is one of the classes.

This class can be used to sort lists or arrays of files (File objects) by directories and files. The File class’s isDirectory() method is used for comparing two files. 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 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 DirectoryFileComparator class. The first example uses the sort() method to sort an array of files. The second example uses the field DIRECTORY_REVERSE as a comparator to perform a reverse sort.

1. A Sort Example

This example sorts files in a directory by using the DirectoryFileComparator‘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 DirectoryFileComparator class’s sort() method.

Note that a real directory and files are required to run the example.

1.1. The Code

DirFileComparatorExample1.java

import org.apache.commons.io.comparator.DirectoryFileComparator;
import java.io.File;

public class DirFileComparatorExample1 {

    public static void main(String [] args) {

        DirectoryFileComparator comparator = new DirectoryFileComparator();

        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) {

            if (file.isDirectory()) {

                System.out.println("DIR: " + file);
            }
            else {
                System.out.println("     " + file);
            }
        }

        System.out.println("");
    }
}

1.2. The Output

### Input files ###
DIR: X:\testdir\classes
DIR: X:\testdir\documents
     X:\testdir\Exception Handling in Web Applications.webarchive
     X:\testdir\glassfish quick-start-guide.pdf
DIR: X:\testdir\install
DIR: X:\testdir\Misc
     X:\testdir\READ ME.txt
     X:\testdir\screenshots.docx
DIR: X:\testdir\source
     X:\testdir\VP-UML_Users_Guide.pdf

### Array sorted ###
DIR: X:\testdir\classes
DIR: X:\testdir\documents
DIR: X:\testdir\install
DIR: X:\testdir\Misc
DIR: X:\testdir\source
     X:\testdir\Exception Handling in Web Applications.webarchive
     X:\testdir\glassfish quick-start-guide.pdf
     X:\testdir\READ ME.txt
     X:\testdir\screenshots.docx
     X:\testdir\VP-UML_Users_Guide.pdf

From the output:

  • Input files: Note that the input files are listed by their names. In the output the suffix DIR: specifies that the file is a directory.
  • Array sorted: The output shows the sorted array of files by directories and files.

2. A Reverse Sort Example

DirectoryFileComparator class defines two fields of type Comparator<File>DIRECTORY_COMPARATOR and DIRECTORY_REVERSE. These two comparators can be used to sort or reverse sort arrays or collections of files.

This example uses the field DIRECTORY_REVERSE to reverse sort an array of files.

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 an 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

DirFileComparatorExample2.java

import org.apache.commons.io.comparator.DirectoryFileComparator;
import java.io.File;
import java.util.Arrays;

public class DirFileComparatorExample2 {

    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, DirectoryFileComparator.DIRECTORY_REVERSE);
        printArrayContents(files);
    }

    private static void printArrayContents(File [] files) {

        for (File file : files) {

            if (file.isDirectory()) {

                System.out.println("DIR: " + file);
            }
            else {
                System.out.println("     " + file);
            }
        }

        System.out.println("");
    }
}

2.2. The Output

### Input files ###
DIR: X:\testdir\classes
DIR: X:\testdir\documents
     X:\testdir\Exception Handling in Web Applications.webarchive
     X:\testdir\glassfish quick-start-guide.pdf
DIR: X:\testdir\install
DIR: X:\testdir\Misc
     X:\testdir\READ ME.txt
     X:\testdir\screenshots.docx
DIR: X:\testdir\source
     X:\testdir\VP-UML_Users_Guide.pdf

### Array reverse sorted ###
     X:\testdir\Exception Handling in Web Applications.webarchive
     X:\testdir\glassfish quick-start-guide.pdf
     X:\testdir\READ ME.txt
     X:\testdir\screenshots.docx
     X:\testdir\VP-UML_Users_Guide.pdf
DIR: X:\testdir\classes
DIR: X:\testdir\documents
DIR: X:\testdir\install
DIR: X:\testdir\Misc
DIR: X:\testdir\source

From the output:

  • Input files: Note that the input files are listed by their names. In the output the suffix DIR: specifies that the file is a directory.
  • Array reverse sorted: The output shows the reverse sorted array of files by directories and files.

3. Download Java Source Code

This was an example of org.apache.commons.io.comparator.DirectoryFileComparator

Download
You can download the full source code of this example here: DirFileComparatorExample.zip

Prasad Saya

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.
Subscribe
Notify of
guest

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

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button