can java list file and sort it by modified time.

G

gdevah

Hi, I am looking for efficient api to implement "ls -lrt" command in
java,
Thanks in advance
Regards,
Devah
 
N

Nigel Wade

Hi, I am looking for efficient api to implement "ls -lrt" command in
java,

The File class can handle the modification time. The sorting can be done by a
List in conjunction with Collections.sort(). As for efficient, what is your
definition of "efficient"?
 
D

Daniel Pitts

Hi, I am looking for efficient api to implement "ls -lrt" command in
java,
Thanks in advance
Regards,
Devah
Learning by example:
public class ModifiedFileList {
public static void main(String[] args) {
File[] files = new File(args[0]).listFiles();
Arrays.sort(files, new Comparator<File>() {
public int compare(File o1, File o2) {
return o1.lastModified() < o2.lastModified() ? -1 :
o1.lastModified() == o2.lastModified() ? 0 :
1;
}
});
for (File file: files) {
System.out.println(file.lastModified() + ": " +
file.toString());
}
}

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top