Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

getting all the files from a directory for latest date,time

 
Sonal Sharma
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to read the latest files in a directory by comparing its date and time of last creation or modification.Can somebody suggest a way to do it?
Any code snippet or tutorial will be highly appreciated.Please try to explain in detail.



Thanks and regard
--Sonal
 
Rob Spoor
Sheriff
Posts: 20827
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
File.listFiles is the method, then call the lastModified() method on each File to get the modification date. Please note that the creation date is NOT available.

The comparison is quite easy; check java.utils.Arrays which has a sort method, you'll need to create a Comparator implementation for that which compares on lastModified().
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!