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