wood burning stoves 2.0*
The moose likes Java in General and the fly likes how to sort files in a Directory based on Date and Time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "how to sort files in a Directory based on Date and Time " Watch "how to sort files in a Directory based on Date and Time " New topic
Author

how to sort files in a Directory based on Date and Time

Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
can anyone tell me how to sort files in a Directory based on Date and Time?

thanks in advance
Ramesh


Ramesh Shanmugam - SCJP 1.5
Sujith Kanaparthi
Ranch Hand

Joined: Sep 04, 2005
Posts: 45
The public long lastModified() of java.io.File can used for sorting based on date and time of last modification of file

Regards,
sujith
Ramesh Shanmugam
Ranch Hand

Joined: Sep 13, 2004
Posts: 132
Hi sujith,

thanks for the reply, can you pls send me code snippet to develop this

Thanks
Ramesh
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14193
    
  20

Ramesh, look at the API documentation of class java.io.File. It contains methods like list() and listFiles() to get an array of File objects that represent the files in the directory.

After getting the array you can sort it using Arrays.sort(), you should implement a Comparator object to compare the files by date.

Please try writing some code yourself, and if you get stuck, show us your code and tell us where you get stuck.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to sort files in a Directory based on Date and Time