Example #1
0
 /**
  * 列出指定目录下的所有子目录
  *
  * @param startDirPath the start dir path
  * @param excludeDirs the exclude dirs
  * @param sortType the sort type
  * @return file [ ]
  */
 public static File[] listDirs(String startDirPath, String[] excludeDirs, SortType sortType) {
   LogUtils.debug(String.format("list dir %s", startDirPath));
   ArrayList<File> dirList = new ArrayList<File>();
   File startDir = new File(startDirPath);
   if (!startDir.isDirectory()) {
     return new File[0];
   }
   File[] dirs =
       startDir.listFiles(
           new FileFilter() {
             public boolean accept(File f) {
               if (f == null) {
                 return false;
               }
               if (f.isDirectory()) {
                 return true;
               }
               return false;
             }
           });
   if (dirs == null) {
     return new File[0];
   }
   if (excludeDirs == null) {
     excludeDirs = new String[0];
   }
   for (File dir : dirs) {
     File file = dir.getAbsoluteFile();
     if (!Arrays.deepToString(excludeDirs).contains(file.getName())) {
       dirList.add(file);
     }
   }
   if (sortType.equals(SortType.BY_NAME_ASC)) {
     Collections.sort(dirList, new SortByName());
   } else if (sortType.equals(SortType.BY_NAME_DESC)) {
     Collections.sort(dirList, new SortByName());
     Collections.reverse(dirList);
   } else if (sortType.equals(SortType.BY_TIME_ASC)) {
     Collections.sort(dirList, new SortByTime());
   } else if (sortType.equals(SortType.BY_TIME_DESC)) {
     Collections.sort(dirList, new SortByTime());
     Collections.reverse(dirList);
   } else if (sortType.equals(SortType.BY_SIZE_ASC)) {
     Collections.sort(dirList, new SortBySize());
   } else if (sortType.equals(SortType.BY_SIZE_DESC)) {
     Collections.sort(dirList, new SortBySize());
     Collections.reverse(dirList);
   } else if (sortType.equals(SortType.BY_EXTENSION_ASC)) {
     Collections.sort(dirList, new SortByExtension());
   } else if (sortType.equals(SortType.BY_EXTENSION_DESC)) {
     Collections.sort(dirList, new SortByExtension());
     Collections.reverse(dirList);
   }
   return dirList.toArray(new File[dirList.size()]);
 }
Example #2
0
 /**
  * 列出指定目录下的所有文件
  *
  * @param startDirPath the start dir path
  * @param filterPattern the filter pattern
  * @param sortType the sort type
  * @return the file [ ]
  */
 public static File[] listFiles(
     String startDirPath, final Pattern filterPattern, SortType sortType) {
   LogUtils.debug(String.format("list file %s", startDirPath));
   ArrayList<File> fileList = new ArrayList<File>();
   File f = new File(startDirPath);
   if (!f.isDirectory()) {
     return new File[0];
   }
   File[] files =
       f.listFiles(
           new FileFilter() {
             public boolean accept(File f) {
               if (f == null) {
                 return false;
               }
               if (f.isDirectory()) {
                 return false;
               }
               if (filterPattern == null) {
                 return true;
               }
               return filterPattern.matcher(f.getName()).find();
             }
           });
   if (files == null) {
     return new File[0];
   }
   for (File file : files) {
     fileList.add(file.getAbsoluteFile());
   }
   if (sortType.equals(SortType.BY_NAME_ASC)) {
     Collections.sort(fileList, new SortByName());
   } else if (sortType.equals(SortType.BY_NAME_DESC)) {
     Collections.sort(fileList, new SortByName());
     Collections.reverse(fileList);
   } else if (sortType.equals(SortType.BY_TIME_ASC)) {
     Collections.sort(fileList, new SortByTime());
   } else if (sortType.equals(SortType.BY_TIME_DESC)) {
     Collections.sort(fileList, new SortByTime());
     Collections.reverse(fileList);
   } else if (sortType.equals(SortType.BY_SIZE_ASC)) {
     Collections.sort(fileList, new SortBySize());
   } else if (sortType.equals(SortType.BY_SIZE_DESC)) {
     Collections.sort(fileList, new SortBySize());
     Collections.reverse(fileList);
   } else if (sortType.equals(SortType.BY_EXTENSION_ASC)) {
     Collections.sort(fileList, new SortByExtension());
   } else if (sortType.equals(SortType.BY_EXTENSION_DESC)) {
     Collections.sort(fileList, new SortByExtension());
     Collections.reverse(fileList);
   }
   return fileList.toArray(new File[fileList.size()]);
 }