How to sort file names in ascending order?

前端 未结 9 2052
孤独总比滥情好
孤独总比滥情好 2020-12-05 10:30

I have a set of files in a folder, and all of them starting with a similar name, except one. Here is an example:

Coordinate.txt
Spectrum_1.txt
Spectrum_2.txt         


        
9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-05 11:13

    The NameFileComparator class available in Commons IO library that have feature for sorting file array by name,last modified date, size and many more.Files can be sorted in ascending and descending order, with case sensitivity or case insensitivity.

    Import :

    org.apache.commons.io.comparator.NameFileComparator

    Code :

    File directory = new File(".");
    File[] files = directory.listFiles();
    Arrays.sort(files, NameFileComparator.NAME_COMPARATOR)
    

提交回复
热议问题