Android 实现文件夹排序功能的实例代码

按文件名排序

 /**
   * 按文件名排序
   * @param filePath
   */
  public static ArrayList orderByName(String filePath) {
    ArrayList FileNameList = new ArrayList();
    File file = new File(filePath);
    File[] files = file.listFiles();
    List fileList = Arrays.asList(files);
    Collections.sort(fileList, new Comparator() {
      @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 orderByName(String filePath) {
    ArrayList FileNameList = new ArrayList();
    File file = new File(filePath);
    File[] files = file.listFiles();
    List fileList = Arrays.asList(files);
    Collections.sort(fileList, new Comparator() {
      @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 orderByDate(String filePath) {
    ArrayList FileNameList = new ArrayList();
    File file = new File(filePath);
    File[] files = file.listFiles();
    Arrays.sort(files, new Comparator() {
      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 orderBySize(String filePath) {
    ArrayList FileNameList = new ArrayList();
    File file = new File(filePath);
    File[] files = file.listFiles();
    List fileList = Arrays.asList(files);
    Collections.sort(fileList, new Comparator() {
      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 实现文件夹排序功能的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

你可能感兴趣的:(Android 实现文件夹排序功能的实例代码)