Currently there is a project that needs to sort the files in the directory and arrange them by time.
1. Filter the XLS file by ImageFilter
The task of the ImageFilter class object is to provide a method of accept (). Callbacks can improve the flexibility of the program.
FilenameFilter filter = new FilenameFilter () {
Public Boolean Accept (File dir, String filename) {
Return Filename.endswith (". xls");
}
};
2. Get the directory below the suffix is the XLS file, which is stored in the array.
String FilePath = Environment.getexternalstoragedirectory (). GetPath () + "/topseed/";
File Pfile = new file (FilePath);
if (!pfile.exists ())
Pfile.mkdir ();
file[] Files = pfile.listfiles (filter);
3. Obtain specific information from the data and store the obtained files in the Listexcel list
4. Sort by chronological order. By modifying the return values of the class Filecomparator, you can arrange them in different chronological order.
Collections.sort (Listexcel,new filecomparator ());
5. The specific implementation of the Filecomparator class is:
public class Filecomparator implements comparator<fileinfo> {
@Override
public int Compare (FileInfo lhs, FileInfo RHS) {
if (Lhs.lastmodified < rhs.lastmodified)
return 1;
Else
return-1;
}
}
Sort files in the directory on Android by time