import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;
import java.util.Comparator;
import org.apache.commons.io.filefilter.WildcardFileFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LastModifiedFileByPattern {
public static Logger logger = LoggerFactory.getLogger(FileFilterTest.class);
@SuppressWarnings({ "unchecked", "rawtypes" })
public static File getLastModifiedFile(File dir, String wildcard) {
File file = null;
if (dir == null || dir.isDirectory() == false) {
return null;
}
FileFilter fileFilter = new WildcardFileFilter(wildcard);
File[] files = dir.listFiles(fileFilter);
if (files == null || files.length == 0) {
return null;
}
Arrays.sort(files, new Comparator() {
public int compare(Object o1, Object o2) {
if (((File)o1).lastModified() > ((File)o2).lastModified()) {
return -1;
} else if (((File)o1).lastModified() < ((File)o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
file = files[0];
return file;
}
public static void main(String[] args) {
File dir = new File("/somepath/");
String wildcard = "service_*.log";
File f = getLastModifiedFile(dir, wildcard);
logger.debug(f.toString());
}
}
댓글 없음:
댓글 쓰기