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

后端 未结 17 921
-上瘾入骨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:20

    I came to this post when i was searching for the same issue but in android. I don't say this is the best way to get sorted files by last modified date, but its the easiest way I found yet.

    Below code may be helpful to someone-

    File downloadDir = new File("mypath");    
    File[] list = downloadDir.listFiles();
        for (int i = list.length-1; i >=0 ; i--) {
            //use list.getName to get the name of the file
        }
    

    Thanks

提交回复
热议问题