Categories:
Audio (13)
Biotech (29)
Bytecode (36)
Database (77)
Framework (7)
Game (7)
General (507)
Graphics (53)
I/O (35)
IDE (2)
JAR Tools (102)
JavaBeans (21)
JDBC (121)
JDK (426)
JSP (20)
Logging (108)
Mail (58)
Messaging (8)
Network (84)
PDF (97)
Report (7)
Scripting (84)
Security (32)
Server (121)
Servlet (26)
SOAP (24)
Testing (54)
Web (15)
XML (322)
Collections:
Other Resources:
Java Example of org.apache.commons.io.comparator
Where can I find an example Java code that uses the org.apache.commons.io.comparator package?
✍: FYIcenter.com
org.apache.commons.io.comparator package
provides various java.util.Comparator implementations for java.io.File.
These comparators can be used to sort lists and arrays of files, for example.
Prasad Saya provided a good Java example that uses the org.apache.commons.io.comparator package at javacodegeeks.com Website.
// Source: javacodegeeks.com
// Supports commons-io-2.6
// Supports commons-io-2.5
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("C:\\fyicenter\\commons-io-2.6\\");
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("");
}
}
You can compile and run the above example in a command window as shown below:
C:\fyicenter>javac -cp C:\fyicenter\commons-io-2.6\commons-io-2.6.jar LastModFileComparatorExample.java C:\fyicenter>java -cp .;C:\fyicenter\commons-io-2.6\commons-io-2.6.jar LastModFileComparatorExample ### Input files ### 2017 Oct 15 12:00 commons-io-2.6-javadoc.jar 2017 Oct 15 12:00 commons-io-2.6-test-sources.jar 2017 Oct 15 12:00 commons-io-2.6-tests.jar 2017 Oct 15 12:00 commons-io-2.6.jar 2017 Oct 15 12:00 docs 2017 Jun 06 22:21 LICENSE.txt 2017 Oct 14 13:57 NOTICE.txt 2017 Oct 15 11:52 RELEASE-NOTES.txt ### Array sorted ### 2017 Jun 06 22:21 LICENSE.txt 2017 Oct 14 13:57 NOTICE.txt 2017 Oct 15 11:52 RELEASE-NOTES.txt 2017 Oct 15 12:00 commons-io-2.6.jar 2017 Oct 15 12:00 docs 2017 Oct 15 12:00 commons-io-2.6-javadoc.jar 2017 Oct 15 12:00 commons-io-2.6-test-sources.jar 2017 Oct 15 12:00 commons-io-2.6-tests.jar
⇒ Java Example of org.apache.commons.io.monitor
⇐ Java Example of org.apache.commons.io.filefilter
2017-04-28, ∼3692🔥, 0💬
Popular Posts:
How to download and install ojdbc11.jar for Oracle 21c? ojdbc11.jar for Oracle 21c is a Java JDBC Dr...
JDK 11 jdk.internal.vm.compiler .jmodis the JMOD file for JDK 11 Internal VM Compiler module. JDK 11...
JDK 11 java.security.sasl.jmod is the JMOD file for JDK 11 Security SASL (Simple Authentication and ...
JDK 17 jdk.internal.vm.ci.jmod is the JMOD file for JDK 17 Internal VM CI module. JDK 17 Internal VM...
JDK 11 jdk.internal.JVM Stat.jmod is the JMOD file for JDK 11 Internal Jvmstat module. JDK 11 Intern...