I have a program that generates reports. It places every report in the same directory. Each report is a different file, with a naming syntax: report*number*.txt
Java: print contents of text file to screen. The latest report will have the largest number. How do I have my program find the latest report and output it to the screen when the application is executed?
问问题
118 次
1 回答
1
you can get the latest modified file, try this:
public static File lastFileModified(String dir) {
File fl = new File(dir);
File[] files = fl.listFiles(new FileFilter() {
public boolean accept(File file) {
return file.isFile();
}
});
long lastMod = Long.MIN_VALUE;
File choise = null;
for (File file : files) {
if (file.lastModified() > lastMod) {
choise = file;
lastMod = file.lastModified();
}
}
return choise;
}
于 2013-03-30T18:55:33.533 回答