java 取目录文件按日期排序

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

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

以下是代码:

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

public class Log {
	public static void main(String[] args) {
		File file = new File("d:/");
		File[] fs = file.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;
		}
	}
}
转自: http://blog.csdn.net/luoyahu/article/details/3926020

你可能感兴趣的:(Android)