posts - 262,  comments - 221,  trackbacks - 0
          【一】DirectoryWalker介紹

          在日常的操作中,我們經(jīng)常需要對(duì)某個(gè)目錄下的資源進(jìn)行遍歷。比如說:遍歷工程目錄,找出所有.svn的目錄并進(jìn)行刪除。這時(shí)我們?nèi)绻褂们懊娴腇ile filter,將不能做到遞歸的效果。于是Apache commons IO提供了一個(gè)用于遍歷目錄進(jìn)行操作的抽象類:DirectoryWalker。

          Abstract class that walks through a directory hierarchy and provides subclasses with convenient hooks to add specific behaviour.

          This class operates with a FileFilter and maximum depth to limit the files and direcories visited. Commons IO supplies many common filter implementations in the filefilter package.

          【二】DirectoryWalker實(shí)戰(zhàn)

          在這個(gè)類的API中,作者演示了如何使用這個(gè)類來刪除指定目錄下的所有文件和.svn目錄,并返回已刪除的文件

          public class FileCleaner extends DirectoryWalker {

           
          public FileCleaner() {
                
          super();
              }


           
          public List clean(File startDirectory) {
                List results 
          = new ArrayList();
                walk(startDirectory, results);
                
          return results;
              }


              
          protected boolean handleDirectory(File directory, int depth, Collection results) {
                
          // delete svn directories and then skip
                if (".svn".equals(directory.getName())) {
                    directory.delete();
                   
          return false;
                }
           else {
                   
          return true;
                }


              }


              
          protected void handleFile(File file, int depth, Collection results) {
                
          // delete file and add to list of deleted
                  file.delete();
                  results.add(file);
              }

            }

          要使用DirectoryWalker來遍歷一個(gè)目錄并進(jìn)行相應(yīng)的操作,步驟如下:

          ①創(chuàng)建一個(gè)繼續(xù)于DirectoryWalker的類,并在構(gòu)造方法中調(diào)用super();
          ②定義一個(gè)業(yè)務(wù)入口方法,例如clean。在這個(gè)方法中調(diào)用walk方法
          ③覆蓋DirectoryWalker類的handleDirectory方法,提供相應(yīng)的處理邏輯
          ④覆蓋DirectoryWalker類的handleFile方法,提供相應(yīng)的處理邏輯

          其中的關(guān)鍵在于walk方法,這個(gè)方法指定了入口的路徑,并提供一個(gè)用于接收處理結(jié)果的參數(shù)。在這個(gè)方法的內(nèi)部,它遞歸地對(duì)每一個(gè)目錄對(duì)象調(diào)用handleDirectory方法,對(duì)于每一個(gè)文件對(duì)象調(diào)用handleFile方法。

          private void walk(File directory, int depth, Collection results) throws IOException {
                  checkIfCancelled(directory, depth, results);
                  
          if (handleDirectory(directory, depth, results)) {
                      handleDirectoryStart(directory, depth, results);
                      
          int childDepth = depth + 1;
                      
          if (depthLimit < 0 || childDepth <= depthLimit) {
                          checkIfCancelled(directory, depth, results);
                          File[] childFiles 
          = (filter == null ? directory.listFiles() : directory.listFiles(filter));
                          
          if (childFiles == null{
                              handleRestricted(directory, childDepth, results);
                          }
           else {
                              
          for (int i = 0; i < childFiles.length; i++{
                                  File childFile 
          = childFiles[i];
                                  
          if (childFile.isDirectory()) {
                                      walk(childFile, childDepth, results);
                                  }
           else {
                                      checkIfCancelled(childFile, childDepth, results);
                                      handleFile(childFile, childDepth, results);
                                      checkIfCancelled(childFile, childDepth, results);
                                  }

                              }

                          }

                      }

                      handleDirectoryEnd(directory, depth, results);
                  }

                  checkIfCancelled(directory, depth, results);
              }


          -------------------------------------------------------------
          生活就像打牌,不是要抓一手好牌,而是要盡力打好一手爛牌。
          posted on 2010-03-09 00:26 Paul Lin 閱讀(3546) 評(píng)論(1)  編輯  收藏 所屬分類: J2SE


          FeedBack:
          # re: 【Java基礎(chǔ)專題】IO與文件讀寫---使用Apache commons IO包進(jìn)行資源遍歷
          2010-03-11 14:48 | swinging
          super()會(huì)被子類自動(dòng)調(diào)用,不一定要顯示表示  回復(fù)  更多評(píng)論
            
          <2010年3月>
          28123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(21)

          隨筆分類

          隨筆檔案

          BlogJava熱點(diǎn)博客

          好友博客

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 永宁县| 绥宁县| 商河县| 长寿区| 江城| 汉川市| 星座| 宁城县| 临夏县| 乌兰县| 平和县| 宜城市| 井冈山市| 北海市| 益阳市| 织金县| 蕉岭县| 凌云县| 亳州市| 阿尔山市| 巴彦县| 综艺| 洛隆县| 寻乌县| 朔州市| 噶尔县| 广昌县| 手机| 安阳县| 右玉县| 南丰县| 库车县| 开封县| 甘德县| 勃利县| 亚东县| 城固县| 永年县| 北票市| 开封市| 郧西县|