Java 遍曆指定檔案夾及子檔案夾下的檔案

來源:互聯網
上載者:User

標籤:

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 遍曆指定檔案夾及子檔案夾下的檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.