Home » Core Java » apache » commons » io » comparator » LastModifiedFileComparator » org.apache.commons.io.comparator.LastModifiedFileComparator 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.LastModifiedFileComparator 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. LastModifiedFileComparator is one of the classes.

This class can be used to sort lists or arrays of files (File objects) by their last modified date/time. The File class’s lastModified() method is used for comparing two files; the method returns the last modified time of the file as a long.

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.

This article shows a usage example. The example is tested on Windows operating system. The Apache Commons IO 2.4 API usage requires Java SE 6 or greater.

1. An Example

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

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

1.1. The Code

LastModFileComparatorExample.java

import org.apache.commons.io.comparator.LastModifiedFileComparator;
import java.io.File;
import java.util.Date;
import java.text.SimpleDateFormat;

public class LastModFileComparatorExample {

    public static void main(String [] args) {

        LastModifiedFileComparator comparator = new LastModifiedFileComparator();

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

        SimpleDateFormat formatter = new SimpleDateFormat("yyyy MMM dd HH:mm");

        for (File file : files) {

            Date date = new Date(file.lastModified());
            System.out.println(formatter.format(date) + "    " + file.getName());
        }

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

1.2. The Output

### Input files ###
2012 Nov 17 13:30    A Difinitive Guide To HTML5.pdf
2014 Dec 15 14:52    BoxingExample.java
2013 May 15 12:45    jdbc4.0-spec.pdf
2014 Nov 17 15:45    screenshots.docx
2012 Jun 17 10:41    Upgrade to Java SE 7 Programmer.webarchive
2014 Dec 10 16:38    WatchEventExample.java

### Array sorted ###
2012 Jun 17 10:41    Upgrade to Java SE 7 Programmer.webarchive
2012 Nov 17 13:30    A Difinitive Guide To HTML5.pdf
2013 May 15 12:45    jdbc4.0-spec.pdf
2014 Nov 17 15:45    screenshots.docx
2014 Dec 10 16:38    WatchEventExample.java
2014 Dec 15 14:52    BoxingExample.java

From the output:

  • Input files: Note that the input files are listed by their names.
  • Array sorted: The output shows the sorted array of files ordered by their last modified date and time.

2. Download Java Source Code

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

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

 

Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments