資料夾 : 檔案根據最新的日期排序,從大到下
阿新 • • 發佈:2022-04-22
/** * 按日期排序 * @param fliePath 檔名稱 * @return File 最新的檔案 */ public static File orderByDate(File file) { // File file = new File(fliePath); File[] fs = file.listFiles(); File nowGetFile =null; Arrays.sort(fs, new Comparator<File>() {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; } }); if(fs.length>0){ System.err.println(fs[fs.length-1].getName() +" 時間 :"+ new Date(fs[fs.length-1].lastModified())); nowGetFile = fs[fs.length-1];//獲取最新的EMS檔案 }//按時間輸出資料夾下面的所有檔案 // for (int i = fs.length - 1; i > -1; i--) { // System.out.println(fs[i].getName()); // System.out.println(new Date(fs[i].lastModified())); // } return nowGetFile; }