经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » Android » 查看文章
Android 之文件夹排序
来源:cnblogs  作者:李培能  时间:2018/9/25 20:00:18  对本文有异议
  1. 按文件名排序

 

  1. /**
  2. * 按文件名排序
  3. * @param filePath
  4. */
  5. public static ArrayList<String> orderByName(String filePath) {
  6. ArrayList<String> FileNameList = new ArrayList<String>();
  7. File file = new File(filePath);
  8. File[] files = file.listFiles();
  9. List fileList = Arrays.asList(files);
  10. Collections.sort(fileList, new Comparator<File>() {
  11. @Override
  12. public int compare(File o1, File o2) {
  13. if (o1.isDirectory() && o2.isFile())
  14. return -1;
  15. if (o1.isFile() && o2.isDirectory())
  16. return 1;
  17. return o1.getName().compareTo(o2.getName());
  18. }
  19. });
  20. for (File file1 : files) {
  21. if (file1.isDirectory()) {
  22. FileNameList.add(file1.getName());
  23. }
  24. }
  25. return FileNameList;
  26. }

 

基于名称:

  1. /**
  2. * 按文件名排序
  3. * @param filePath
  4. */
  5. public static ArrayList<String> orderByName(String filePath) {
  6. ArrayList<String> FileNameList = new ArrayList<String>();
  7. File file = new File(filePath);
  8. File[] files = file.listFiles();
  9. List fileList = Arrays.asList(files);
  10. Collections.sort(fileList, new Comparator<File>() {
  11. @Override
  12. public int compare(File o1, File o2) {
  13. if (o1.isDirectory() && o2.isFile())
  14. return -1;
  15. if (o1.isFile() && o2.isDirectory())
  16. return 1;
  17. return o1.getName().compareTo(o2.getName());
  18. }
  19. });
  20. for (File file1 : files) {
  21. if (file1.isDirectory()) {
  22. FileNameList.add(file1.getName());
  23. }
  24. }
  25. return FileNameList;
  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
  • 26

基于最近修改时间:

  1. /**
  2. * 按文件修改时间排序
  3. * @param filePath
  4. */
  5. public static ArrayList<String> orderByDate(String filePath) {
  6. ArrayList<String> FileNameList = new ArrayList<String>();
  7. File file = new File(filePath);
  8. File[] files = file.listFiles();
  9. Arrays.sort(files, new Comparator<File>() {
  10. public int compare(File f1, File f2) {
  11. long diff = f1.lastModified() - f2.lastModified();
  12. if (diff > 0)
  13. return 1;
  14. else if (diff == 0)
  15. return 0;
  16. else
  17. return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
  18. }
  19. public boolean equals(Object obj) {
  20. return true;
  21. }
  22. });
  23. for (File file1 : files) {
  24. if (file1.isDirectory()) {
  25. FileNameList.add(file1.getName());
  26. }
  27. }
  28. return FileNameList;
  29. }
  • 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

基于大小:

  1. /**
  2. * 按文件大小排序
  3. * @param filePath
  4. */
  5. public static ArrayList<String> orderBySize(String filePath) {
  6. ArrayList<String> FileNameList = new ArrayList<String>();
  7. File file = new File(filePath);
  8. File[] files = file.listFiles();
  9. List<File> fileList = Arrays.asList(files);
  10. Collections.sort(fileList, new Comparator<File>() {
  11. public int compare(File f1, File f2) {
  12. long s1 = getFolderSize(f1);
  13. long s2 = getFolderSize(f2);
  14. long diff = s1 - s2;
  15. if (diff > 0)
  16. return 1;
  17. else if (diff == 0)
  18. return 0;
  19. else
  20. return -1;// 如果 if 中修改为 返回-1 同时此处修改为返回 1 排序就会是递减
  21. }
  22. public boolean equals(Object obj) {
  23. return true;
  24. }
  25. });
  26. for (File file1 : files) {
  27. if (file1.isDirectory()) {
  28. FileNameList.add(file1.getName());
  29. }
  30. }
  31. return FileNameList;
  32. }
  33. /**
  34. * 获取文件夹大小
  35. * @param file File实例
  36. * @return long
  37. */
  38. public static long getFolderSize(File file) {
  39. long size = 0;
  40. try {
  41. java.io.File[] fileList = file.listFiles();
  42. for (int i = 0; i < fileList.length; i++) {
  43. if (fileList[i].isDirectory()) {
  44. size = size + getFolderSize(fileList[i]);
  45. } else {
  46. size = size + fileList[i].length();
  47. }
  48. }
  49. } catch (Exception e) {
  50. e.printStackTrace();
  51. }
  52. return size;
  53. }
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号