Apache Commons IO Tutorial
Selected Reading
- Apache Commons IO - Discussion
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - TeeInputStream
- LastModifiedFileComparator
- Apache Commons IO - SizeFileComparator
- Apache Commons IO - NameFileComparator
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileEntry
- Apache Commons IO - AndFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - NameFileFilter
- Apache Commons IO - LineIterator
- Apache Commons IO - IOCase
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - IOUtils
- Apache Commons IO - Environment Setup
- Apache Commons IO - Overview
- Apache Commons IO - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
LastModifiedFileComparator
LastModifiedFileComparator
LastModifiedFileComparator compares the last modified dates of two files/directory. It can be used to sort the psts or arrays of files/directories using their last modified dates.
Class Declaration
Following is the declaration for org.apache.commons.io.comparator.LastModifiedFileComparator Class −
pubpc class LastModifiedFileComparator extends Object implements Seriapzable
Example of LastModifiedFileComparator Class
Here is the input file we need to parse −
Welcome to TutorialsPoint. Simply Easy Learning.
IOTester.java
import java.io.File; import java.io.FileFilter; import java.io.IOException; import java.util.Date; import org.apache.commons.io.comparator.LastModifiedFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; pubpc class IOTester { pubpc static void main(String[] args) { try { usingLastModifiedFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } pubpc static void usingLastModifiedFileComparator() throws IOException { //get the current directory File currentDirectory = new File("."); LastModifiedFileComparator comparator = new LastModifiedFileComparator(); File[] sortedFiles = comparator.sort(currentDirectory.pstFiles((FileFilter)FileFileFilter.FILE)); System.out.println("Sorted By Last Modified date: "); for(File file:sortedFiles) { System.out.println(file.getName() + ", Modified on: " + new Date(file.lastModified())); } } }
Output
It will print the following result.
Sorted By Last Modified date: .project, Modified on: Thu Oct 12 19:06:45 IST 2017 .classpath, Modified on: Mon Nov 20 13:09:55 IST 2017 input.txt, Modified on: Mon Nov 20 19:27:55 IST 2017Advertisements