Best way to list files in Java, sorted by Date Modified?

后端 未结 17 858
-上瘾入骨i
-上瘾入骨i 2020-11-22 11:51

I want to get a list of files in a directory, but I want to sort it such that the oldest files are first. My solution was to call File.listFiles and just resort the list ba

相关标签:
17条回答
  • 2020-11-22 12:38

    Imports :

    org.apache.commons.io.comparator.LastModifiedFileComparator
    

    Apache Commons

    Code :

    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);
    
        }
    
    0 讨论(0)
  • 2020-11-22 12:40

    You can use Apache LastModifiedFileComparator library

     import org.apache.commons.io.comparator.LastModifiedFileComparator;  
    
    
    File[] files = directory.listFiles();
            Arrays.sort(files, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
            for (File file : files) {
                Date lastMod = new Date(file.lastModified());
                System.out.println("File: " + file.getName() + ", Date: " + lastMod + "");
            }
    
    0 讨论(0)
  • 2020-11-22 12:40
    private static List<File> sortByLastModified(String dirPath) {
        List<File> files = listFilesRec(dirPath);
        Collections.sort(files, new Comparator<File>() {
            public int compare(File o1, File o2) {
                return Long.compare(o1.lastModified(), o2.lastModified());
            }
        });
        return files;
    }
    
    0 讨论(0)
  • 2020-11-22 12:42

    You might also look at apache commons IO, it has a built in last modified comparator and many other nice utilities for working with files.

    0 讨论(0)
  • 2020-11-22 12:42

    There is a very easy and convenient way to handle the problem without any extra comparator. Just code the modified date into the String with the filename, sort it, and later strip it off again.

    Use a String of fixed length 20, put the modified date (long) into it, and fill up with leading zeros. Then just append the filename to this string:

    String modified_20_digits = ("00000000000000000000".concat(Long.toString(temp.lastModified()))).substring(Long.toString(temp.lastModified()).length()); 
    
    result_filenames.add(modified_20_digits+temp.getAbsoluteFile().toString());
    

    What happens is this here:

    Filename1: C:\data\file1.html Last Modified:1532914451455 Last Modified 20 Digits:00000001532914451455

    Filename1: C:\data\file2.html Last Modified:1532918086822 Last Modified 20 Digits:00000001532918086822

    transforms filnames to:

    Filename1: 00000001532914451455C:\data\file1.html

    Filename2: 00000001532918086822C:\data\file2.html

    You can then just sort this list.

    All you need to do is to strip the 20 characters again later (in Java 8, you can strip it for the whole Array with just one line using the .replaceAll function)

    0 讨论(0)
提交回复
热议问题