Android 之文件夹排序
程序员文章站
2023-11-18 14:22:46
基于名称: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 基于最近修改时间: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ......
按文件名排序
/** * 按文件名排序 * @param filepath */ public static arraylist<string> orderbyname(string filepath) { arraylist<string> filenamelist = new arraylist<string>(); file file = new file(filepath); file[] files = file.listfiles(); list filelist = arrays.aslist(files); collections.sort(filelist, new comparator<file>() { @override public int compare(file o1, file o2) { if (o1.isdirectory() && o2.isfile()) return -1; if (o1.isfile() && o2.isdirectory()) return 1; return o1.getname().compareto(o2.getname()); } }); for (file file1 : files) { if (file1.isdirectory()) { filenamelist.add(file1.getname()); } } return filenamelist; }
基于名称:
/** * 按文件名排序 * @param filepath */ public static arraylist<string> orderbyname(string filepath) { arraylist<string> filenamelist = new arraylist<string>(); file file = new file(filepath); file[] files = file.listfiles(); list filelist = arrays.aslist(files); collections.sort(filelist, new comparator<file>() { @override public int compare(file o1, file o2) { if (o1.isdirectory() && o2.isfile()) return -1; if (o1.isfile() && o2.isdirectory()) return 1; return o1.getname().compareto(o2.getname()); } }); for (file file1 : files) { if (file1.isdirectory()) { filenamelist.add(file1.getname()); } } return filenamelist; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
基于最近修改时间:
/** * 按文件修改时间排序 * @param filepath */ public static arraylist<string> orderbydate(string filepath) { arraylist<string> filenamelist = new arraylist<string>(); file file = new file(filepath); file[] files = file.listfiles(); arrays.sort(files, new comparator<file>() { public int compare(file f1, file f2) { long diff = f1.lastmodified() - f2.lastmodified(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减 } public boolean equals(object obj) { return true; } }); for (file file1 : files) { if (file1.isdirectory()) { filenamelist.add(file1.getname()); } } return filenamelist; }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
基于大小:
/** * 按文件大小排序 * @param filepath */ public static arraylist<string> orderbysize(string filepath) { arraylist<string> filenamelist = new arraylist<string>(); file file = new file(filepath); file[] files = file.listfiles(); list<file> filelist = arrays.aslist(files); collections.sort(filelist, new comparator<file>() { public int compare(file f1, file f2) { long s1 = getfoldersize(f1); long s2 = getfoldersize(f2); long diff = s1 - s2; if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减 } public boolean equals(object obj) { return true; } }); for (file file1 : files) { if (file1.isdirectory()) { filenamelist.add(file1.getname()); } } return filenamelist; } /** * 获取文件夹大小 * @param file file实例 * @return long */ public static long getfoldersize(file file) { long size = 0; try { java.io.file[] filelist = file.listfiles(); for (int i = 0; i < filelist.length; i++) { if (filelist[i].isdirectory()) { size = size + getfoldersize(filelist[i]); } else { size = size + filelist[i].length(); } } } catch (exception e) { e.printstacktrace(); } return size; }