Android 实现文件夹排序功能的实例代码
程序员文章站
2023-12-03 13:39:34
按文件名排序
/**
* 按文件名排序
* @param filepath
*/
public static arraylist&...
按文件名排序
/** * 按文件名排序 * @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; }
基于最近修改时间:
/** * 按文件修改时间排序 * @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; }
基于大小:
/** * 按文件大小排序 * @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; }
总结
以上所述是小编给大家介绍的android 实现文件夹排序功能的实例代码,希望对大家有所帮助
上一篇: 适合中小型企业的推广方式