楼主没有说明什么最新?是最近创建还是最新修改还是anything else 做了个最新创建的 最新修改直接有api可调的 lastModified()import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;public class Test { private static List dateList = new ArrayList(); private String standardDate = "yyyy-mm-dd";
public static void main(String[] args) { String file = "c:"; Test test = new Test(); System.out.println("The most newly created file is: " + test.getMostNewlyCreatedFile(file)); }
public String getMostNewlyCreatedFile(String file){ try { Process p = Runtime.getRuntime().exec( "cmd /C dir " + file + " /tc"); InputStream is = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result; String createdDate = null;
while ((result = br.readLine()) != null) { if (result.length() >= standardDate.length()) { createdDate = result.substring(0, 11); dateList.add(createdDate); } } } catch (java.io.IOException exc) { exc.printStackTrace(); } Collections.sort(dateList, new StringComparator()); return (String)dateList.get(dateList.size() - 1); } }class StringComparator implements Comparator { public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }
import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List;public class Test { private static List dateList = new ArrayList(); private String standardDate = "yyyy-mm-dd";
public static void main(String[] args) { String file = "c:"; Test test = new Test(); System.out.println("The most newly created file is: " + test.getMostNewlyCreatedFileDate(file)); }
public String getMostNewlyCreatedFileDate(String file){ try { Process p = Runtime.getRuntime().exec( "cmd /C dir " + file + " /tc"); InputStream is = p.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String result; String createdDate = null;
while ((result = br.readLine()) != null) { if (result.length() >= standardDate.length()) { createdDate = result.substring(0, 11); dateList.add(createdDate); } } } catch (java.io.IOException exc) { exc.printStackTrace(); } Collections.sort(dateList, new StringComparator()); return (String)dateList.get(dateList.size() - 1); } }class StringComparator implements Comparator { public int compare(Object o1, Object o2) { return o1.toString().compareTo(o2.toString()); } }
这个是我以前写的可以实现功能,不过效率如何就没具体考量过了,楼主可以参考下: /** * 对指定的File型数组按时间将序进行排序 * */ public static File[] sort(File[] files){ File[] buffer=files; Arrays.sort(files,new Comparator<File>(){ public int compare(File file1, File file2) { return file1.lastModified()<file2.lastModified()?1:-1; } }); return buffer; }
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class Test {
private static List dateList = new ArrayList();
private String standardDate = "yyyy-mm-dd";
public static void main(String[] args) {
String file = "c:";
Test test = new Test();
System.out.println("The most newly created file is: "
+ test.getMostNewlyCreatedFile(file));
}
public String getMostNewlyCreatedFile(String file){
try {
Process p = Runtime.getRuntime().exec(
"cmd /C dir " + file + " /tc"); InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result;
String createdDate = null;
while ((result = br.readLine()) != null) {
if (result.length() >= standardDate.length()) {
createdDate = result.substring(0, 11);
dateList.add(createdDate);
}
}
} catch (java.io.IOException exc) {
exc.printStackTrace();
}
Collections.sort(dateList, new StringComparator());
return (String)dateList.get(dateList.size() - 1);
}
}class StringComparator implements Comparator {
public int compare(Object o1, Object o2) {
return o1.toString().compareTo(o2.toString());
}
}
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;public class Test {
private static List dateList = new ArrayList();
private String standardDate = "yyyy-mm-dd";
public static void main(String[] args) {
String file = "c:";
Test test = new Test();
System.out.println("The most newly created file is: "
+ test.getMostNewlyCreatedFileDate(file));
}
public String getMostNewlyCreatedFileDate(String file){
try {
Process p = Runtime.getRuntime().exec(
"cmd /C dir " + file + " /tc"); InputStream is = p.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String result;
String createdDate = null;
while ((result = br.readLine()) != null) {
if (result.length() >= standardDate.length()) {
createdDate = result.substring(0, 11);
dateList.add(createdDate);
}
}
} catch (java.io.IOException exc) {
exc.printStackTrace();
}
Collections.sort(dateList, new StringComparator());
return (String)dateList.get(dateList.size() - 1);
}
}class StringComparator implements Comparator {
public int compare(Object o1, Object o2) {
return o1.toString().compareTo(o2.toString());
}
}
* 对指定的File型数组按时间将序进行排序
* */
public static File[] sort(File[] files){
File[] buffer=files;
Arrays.sort(files,new Comparator<File>(){
public int compare(File file1, File file2) {
return file1.lastModified()<file2.lastModified()?1:-1;
}
});
return buffer;
}