I am using java.io API's. There are lots of file in my dir for the below pattern.
FilenameFilter fileFilter = new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.matches(filePrefix+"_.*\\."+fileExt);
}
};
String listFiles [] = dirFile.list(fileFilter);
for(int i = 0 ;i < listFiles.length ; i++){
listOfFiles.add(listFiles[i]);
// need only 10 files to processed
if(i == 10){
break;
}
}
I wanted to know that list
method will get listing based on what.I am if i wanted to have time based sorting, for example - if i want 10 files which are oldest /or latest.
How will i do it.
Solution :
Arrays.sort(listFiles, new Comparator<File>() {
@Override
public int compare(File f1, File f2) {
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
}
});
The above only filters . You may be better off using the File.listFiles()
method, which returns a set of File
objects (you can provide an optional filter, as above). These objects will have the attributes that you want to further sort on (perhaps writing a custom Comparator
?). See the doc here and a sorting tutorial here .
您可以使用'listFiles(fileFilter)'
列出文件对象而不是字符串,然后sort/filter
它们进行sort/filter
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.