標籤:
Java 遍曆指定檔案夾及子檔案夾下的檔案
/** * 遍曆指定檔案夾及子檔案夾下的檔案 * * @author testcs_dn * @date2014年12月12日下午2:33:49 * @param file 要遍曆的指定檔案夾 * @param collector 合格結果加入到此List<File>中 * @param pathInclude 路徑中包括指定的字串 * @param fileNameInclude 檔案名稱(不包括副檔名)中包括指定的字串 * @param extnEquals 副檔名為指定字串 * @throws IOException */public static void listFiles(File file,List<File> collector, String pathInclude, String fileNameInclude, String extnEquals) throws IOException {if (file.isFile() && (StringUtils.isBlank(pathInclude) || file.getAbsolutePath().indexOf(pathInclude) != -1)&& (StringUtils.isBlank(fileNameInclude) || file.getName().indexOf(fileNameInclude) != -1)&& (StringUtils.isBlank(extnEquals) || file.getName().endsWith(extnEquals))){collector.add(file);}if((!file.isHidden() && file.isDirectory()) && !isIgnoreFile(file)) {File[] subFiles = file.listFiles();for(int i = 0; i < subFiles.length; i++) {listFiles(subFiles[i],collector, pathInclude, fileNameInclude, extnEquals);}}}
推斷檔案夾是否須要忽略
private static boolean isIgnoreFile(File file) {List<String> ignoreList = new ArrayList<String>();ignoreList.add(".svn");ignoreList.add("CVS");ignoreList.add(".cvsignore");ignoreList.add("SCCS");ignoreList.add("vssver.scc");ignoreList.add(".DS_Store");for(int i = 0; i < ignoreList.size(); i++) {if(file.getName().equals(ignoreList.get(i))) {return true;}}return false;}
Java 遍曆指定檔案夾及子檔案夾下的檔案