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

About Prasad Saya

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.

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 Files. 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

Download
You can download the full source code of this example here: NameFileComparatorExample.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

 

Leave a Reply

avatar

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

  Subscribe  
Notify of