java 取目录文件按日期排序

java 的jdk里的File类没有提供按日期或大小排序来取的目录下文件列表的功能.

所以只能自己写一个了.简单实现.

以下是代码:

public class Log { public static void main(String[] args) { File file = new File("d:/"); File [] fs = fil.listFiles(); Arrays.sort(fs, new Log.CompratorByLastModified()); for (int i = 0; i < fs.length; i++) { System.out.println(new Date(fs[i].lastModified()).toLocaleString()); } } static class CompratorByLastModified implements Comparator { public int compare(File f1, File f2) { long diff = f1.lastModified()-f2.lastModified(); if(diff>0) return 1; else if(diff==0) return 0; else return -1; } public boolean equals(Object obj){ return true; } } }

你可能感兴趣的:(java,java,file,equals,class,object,string)