【发布时间】:2011-06-12 07:02:03
【问题描述】:
我有一种方法可以从目录中的文件中读取内容。但出于功能原因,必须从最旧的文件开始(由属性 lastmodified 表示)并以最新的文件结束。
这是我打开和读取文件的代码:
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.matches("access_log.*");
}
};
File folder = new File("/home/myfiles");
File[] listOfFiles = folder.listFiles(filter);
for (int i = 0; i < listOfFiles.length; i++) {
String sFileName = listOfFiles[i].getName();
File accessLogFile = new File(aLog.getPath(), sFileName);
long time=accessLogFile.lastModified();
// do something with the file
}
有没有人可以快速按日期对文件列表进行排序的解决方案?
【问题讨论】:
-
stackoverflow.com/questions/203030/… 是否包含您问题的答案?
-
你试过 FileUtils.dirListByAscendingDate 吗? rgagnon.com/javadetails/java-0606.html
-
您似乎对日志文件感兴趣。这些通常以这样一种方式命名,即按文件名的词法排序也按时间排序。