public class FileSortUtils {
public static void main(String[] args) {
File file = new File("F:\\file");
List<File> list = Arrays.asList(file.listFiles());
list.forEach(f -> System.out.println(f.getName()));
System.out.println("----------");
List<File> sort = sort(list);
sort.forEach(f -> System.out.println(f.getName()));
}
public static List<File> sort(List<File> list) {
List<File> sortFile = new ArrayList<>();
//文件夹集合
List<File> folderList = list.stream().filter(file -> file.isDirectory()).collect(Collectors.toList());
//文件集合
List<File> fileList = list.stream().filter(file -> file.isFile()).collect(Collectors.toList());
//文件夹排序
Collections.sort(folderList, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
Comparator comparator = Collator.getInstance(Locale.CHINA);
return comparator.compare(o1.getName(),o2.getName());
}
});
//文件排序
Collections.sort(fileList, new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
Comparator comparator = Collator.getInstance(Locale.CHINA);
return comparator.compare(o1.getName(),o2.getName());
}
});
//合并
sortFile.addAll(folderList);
sortFile.addAll(fileList);
return sortFile;
}
}
示例:
result.jpg
网友评论