7

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..

nobalG
  • 4,544
  • 3
  • 34
  • 72
Jatin
  • 1,650
  • 3
  • 17
  • 32

8 Answers8

7
    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;
           }
        }
    });
Jclick
  • 89
  • 5
6

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

Community
  • 1
  • 1
Hartmut Pfitzinger
  • 2,304
  • 3
  • 28
  • 48
4

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;
        }
    }

});
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Oyewo Remi
  • 406
  • 5
  • 8
3

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());
    }
}
Akash Moradiya
  • 3,318
  • 1
  • 14
  • 19
  • I have tried this code. I have tried with renaming images, in that case I again lost my original order. newly renamed images comes last. – Jatin Oct 27 '14 at 06:40
  • 1
    Note `LastModifiedFileComparator` is available in Apache's Common IO. – Fred Oct 07 '15 at 22:16
1

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
}
Lúcio Aguiar
  • 191
  • 1
  • 3
  • 6
0

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

Kunchok Tashi
  • 2,413
  • 1
  • 22
  • 30
0

1.add this to build.gradle :

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

2.add code to activity :

  File[] folderFiles = Files.listFiles();

  // Sort files in ascending order base on last modification
  Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);

  // Sort files in descending order base on last modification
  Arrays.sort(folderFiles, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
Hossin Asaadi
  • 367
  • 6
  • 13
0

One quick and elegant way to sort array of files, by date of change is:

        Arrays.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                return Long.compare(f1.lastModified(), f2.lastModified());
                // For descending
                // return -Long.compare(f1.lastModified(), f2.lastModified());
            }
        });

To sort array of files, by name is:

        Arrays.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                return f1.compareTo(f2);
                // For descending
                // return -f1.compareTo(f2);
            }
        });