import java.io.File;
import java.util.Date;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
public class FileListSort {
private static final long serialVersionUID = 7025768684443110109L;
public String getFilePathName() {
TreeMap<Long,File> tm = new TreeMap<Long,File>();
File file = new File("E:\\temp");
File subFile[] = file.listFiles();
int fileNum = subFile.length;
for (int i = 0; i < fileNum; i++) {
Long tempLong = new Long(subFile[i].lastModified());
tm.put(tempLong, subFile[i]);
}
System.out.println("按时间从前到后排序--->");
System.out.println("最早的一个文件的路径-->"+tm.get(tm.firstKey()).getPath());
System.out.println("最近的一个文件的路径-->"+tm.get(tm.lastKey()).getPath());
Set<Long> set = tm.keySet();
Iterator<Long> it = set.iterator();
while (it.hasNext()) {
Object key = it.next();
Object objValue = tm.get(key);
File tempFile = (File) objValue;
Date date=new Date((Long)key);
System.out.println(tempFile.getPath() + "\t"+date);
}
return null;
}
public static void main(String[] args) {
new FileListSort().getFilePathName();
}
}
posted on 2011-12-26 09:40
飞翔天使 阅读(2580)
评论(0) 编辑 收藏 所属分类:
java