Sort files in the directory on Android by time

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.