Java File Name Sort Filter

  1. import java.io.*;
  2. import java.util.*;
  3. public final class FileLister {
  4.     private String dir = "./";
  5.     private String extendedName = "";
  6.     /*anonym inner class acts as the assistant to deal with extended name*/
  7.     private FileFilter filter = new FileFilter(){
  8.         public boolean accept(File f) {
  9.             if (f.isDirectory()) return true;
  10.             return f.getName().lastIndexOf("."+extendedName) != -1;
  11.          }};
  12.     public FileLister (String dir, String ext) {
  13.         this.dir = dir;        extendedName = ext;
  14.     }
  15.     private void walkThrough(File f, List dirLs, List fileLs) {
  16.         File[] fs = f.listFiles(filter);
  17.         for (int i = 0; i < fs.length; i++) {
  18.             if (fs[i].isDirectory()) {
  19.                 walkThrough(fs[i], dirLs, fileLs);
  20.                 dirLs.add(fs[i].getPath());
  21.             }
  22.             else {
  23.                 fileLs.add(fs[i].getPath());
  24.             }
  25.         }
  26.     }
  27.     public String[] listFile(boolean isDirectoryFirst, boolean ascend) {
  28.         List dirLs = new ArrayList();
  29.         List fileLs = new ArrayList();
  30.         walkThrough(new File(dir), dirLs, fileLs);
  31.         Object[] dirs = dirLs.toArray();
  32.         Object[] fs = fileLs.toArray();
  33.         Arrays.sort(dirs);
  34.         Arrays.sort(fs);
  35.         String[] s = new String[dirs.length+fs.length];
  36.         if (isDirectoryFirst && ascend) {//dir fiest and ascend
  37.             for (int j = 0; j < dirs.length; j++) {
  38.                 s[j] = (String)dirs[j];
  39.             }
  40.             for (int k = 0; k < fs.length; k++) {
  41.                 s[dirs.length + k] = (String)fs[k];
  42.             }
  43.         }
  44.         else if (isDirectoryFirst && !ascend) {// dir first and not ascend
  45.             for (int j = 0; j < dirs.length; j++) {
  46.                 s[j] = (String)dirs[dirs.length - j - 1];
  47.             }
  48.             for (int k = 0; k < fs.length; k++) {
  49.                 s[dirs.length + k] = (String)fs[fs.length - k - 1];
  50.             }
  51.         }
  52.         else if (!isDirectoryFirst && ascend) {//files first and ascend
  53.             for (int j = 0; j < fs.length; j++) {
  54.                 s[j] = (String)fs[j];
  55.             }
  56.             for (int k = 0; k < dirs.length; k++) {
  57.                 s[fs.length + k] = (String)dirs[k];
  58.             }
  59.         }
  60.         else {//files first and not ascend
  61.             for (int j = 0; j < fs.length; j++) {
  62.                 s[j] = (String)fs[fs.length - j - 1];
  63.             }
  64.             for (int k = 0; k < dirs.length; k++) {
  65.                 s[fs.length + k] = (String)dirs[dirs.length - k - 1];
  66.             }
  67.         }
  68.         return s;
  69.     }
  70.     public static void main(String[] args) {
  71.         final String errTip = "Usage: java FileLister dir extendedName";
  72.         //validate parameters
  73.         if (args.length != 2 ||  args[0] == null || args[0].length()<1 ||
  74.             args[1] == null || args[1].length()<1 ) {
  75.             System.err.println(errTip);
  76.             System.exit(0);
  77.         }
  78.         String[] s = new FileLister(args[0].trim(),args[1].trim()).listFile(true,true);
  79.         for (int x = 0; x < s.length; x++)
  80.             System.out.println(s[x]);
  81.     }
  82. }
原文地址:https://www.cnblogs.com/cy163/p/1440175.html