public static void main(String[] args) {
File dir = new File("/Users/mff/Documents/books");
File[] files = dir.listFiles();
System.out.println("before sort: ");
for (int i = 0; i < files.length; i++) {
System.out.print(files[i] + " ");
System.out.println();
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
Arrays.sort(files);
System.out.println("sort by name: ");
for (int i = 0; i < files.length; i++) {
System.out.print(files[i] + " ");
System.out.println();
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
Arrays.sort(files, new CompratorBySize());
System.out.println("sort by size: ");
for (int i = 0; i < files.length; i++) {
System.out.print(files[i] + " ");
System.out.println();
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
Arrays.sort(files, new CompratorByLastModified());
System.out.println("sort by last modified: ");
for (int i = 0; i < files.length; i++) {
System.out.print(files[i] + " ");
System.out.println();
}
System.out.println("~~~~~~~~~~~~~~~~~~~~~~");
}
java测试文件夹下文件排序的方法
最新推荐文章于 2023-04-28 10:01:34 发布