sorting files based on its creation date in android-ThrowExceptions

Exception or error:

I want list of file based on my creation date.
When i updating any if images and trying to retrive all images,then orders are changed randomly.

Here is my code,

 File[] files = parentDir.listFiles();
    for (File file : files) {
// I am getting files here
    }

Any help..

How to solve:

I want list of file based on my creation date.

As the two previous answers pointed out, you can sort the files according to the modification date:

file.lastModified()

But the modification date is updated e.g. in the instant of renaming a file. So, this won’t work to represent the creation date.

Unfortunately, the creation date is not available, thus you need to rethink your basic strategy:

see an old answer of CommonsWare

###

    List<File> fileList = new ArrayList<File>();
    Collections.sort(fileList, new Comparator<File>() {

        @Override
        public int compare(File file1, File file2) {
            long k = file1.lastModified() - file2.lastModified();
            if(k > 0){
               return 1;
            }else if(k == 0){
               return 0;
            }else{
              return -1;
           }
        }
    });

###

try this may help you,

public static void main(String[] args) throws IOException {
    File directory = new File(".");
    // get just files, not directories
    File[] files = directory.listFiles((FileFilter) FileFileFilter.FILE);

    System.out.println("Default order");
    displayFiles(files);

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
    System.out.println("\nLast Modified Ascending Order (LASTMODIFIED_COMPARATOR)");
    displayFiles(files);

    Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
    System.out.println("\nLast Modified Descending Order (LASTMODIFIED_REVERSE)");
    displayFiles(files);

}

public static void displayFiles(File[] files) {
    for (File file : files) {
        System.out.printf("File: %-20s Last Modified:" + new Date(file.lastModified()) + "\n", file.getName());
    }
}

###

Here is the code to sort the files according to the modification date as the creation date is not available.

File[] files = parentDir.listFiles();
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;
        }
    }

});

###

In the Kotlin language it can be written like this:

private fun getListFiles(parentDir: File): MutableList<File> {

    val inFiles: MutableList<File> = parentDir.listFiles().toMutableList()

    inFiles.filter { it.extension == "jpg" }

    inFiles.sortByDescending({ it.lastModified()})

    return inFiles
}

###

guys If you are not able to resolve this LastModifiedFileComparator problem, Here is the solution I have found.

Step 1
Open app level build.gradle
and add dependency as below. To get updated version click here

implementation group: 'commons-io', name: 'commons-io', version: '2.0.1'

Step 2
If it did’t work than Add mavenCentral() creating new repositories in your app level build.gradle

repositories{
   mavenCentral()
}
dependencies {
//all implementation

That all It should work like charm, If not please refer here

Leave a Reply

Your email address will not be published. Required fields are marked *