国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android 實(shí)現(xiàn)文件夾排序功能的實(shí)例代碼

2019-10-21 21:38:25
字體:
供稿:網(wǎng)友

按文件名排序

 /**   * 按文件名排序   * @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實(shí)例   * @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;  }

總結(jié)

以上所述是小編給大家介紹的Android 實(shí)現(xiàn)文件夾排序功能的實(shí)例代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對VEVB武林網(wǎng)網(wǎng)站的支持!


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 榆树市| 监利县| 延川县| 天镇县| 渝北区| 哈巴河县| 蓬安县| 金堂县| 东阿县| 门头沟区| 宣化县| 紫金县| 黔南| 博野县| 安西县| 榕江县| 扶风县| 肥西县| 平原县| 吉林市| 格尔木市| 昌黎县| 洛浦县| 永嘉县| 穆棱市| 丘北县| 乌拉特前旗| 卢湾区| 贡觉县| 北辰区| 荥阳市| 霍城县| 西乌珠穆沁旗| 双柏县| 西乌| 洱源县| 五华县| 景泰县| 化德县| 葫芦岛市| 泌阳县|