<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

    在日常的操作中,我們經常需要對某個目錄下的資源進行遍歷。比如說:遍歷工程目錄,找出所有.svn的目錄并進行刪除。這時我們如果使用前面的File filter,將不能做到遞歸的效果。于是Apache commons IO提供了一個用于遍歷目錄進行操作的抽象類: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實戰

    在這個類的API中,作者演示了如何使用這個類來刪除指定目錄下的所有文件和.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來遍歷一個目錄并進行相應的操作,步驟如下:

    ①創建一個繼續于DirectoryWalker的類,并在構造方法中調用super();
    ②定義一個業務入口方法,例如clean。在這個方法中調用walk方法
    ③覆蓋DirectoryWalker類的handleDirectory方法,提供相應的處理邏輯
    ④覆蓋DirectoryWalker類的handleFile方法,提供相應的處理邏輯

    其中的關鍵在于walk方法,這個方法指定了入口的路徑,并提供一個用于接收處理結果的參數。在這個方法的內部,它遞歸地對每一個目錄對象調用handleDirectory方法,對于每一個文件對象調用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 閱讀(3539) 評論(1)  編輯  收藏 所屬分類: J2SE


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

    常用鏈接

    留言簿(21)

    隨筆分類

    隨筆檔案

    BlogJava熱點博客

    好友博客

    搜索

    •  

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲A∨午夜成人片精品网站| 在线观看免费av网站| 在线视频免费观看高清| 久久久久亚洲av无码专区蜜芽| 成全视成人免费观看在线看| 亚洲色一色噜一噜噜噜| 一级黄色片免费观看| 国内精品99亚洲免费高清| 东方aⅴ免费观看久久av| 亚洲成AV人片在线观看WWW| 99精品视频在线视频免费观看| 香蕉蕉亚亚洲aav综合| 69堂人成无码免费视频果冻传媒 | 59pao成国产成视频永久免费| 中文字幕人成人乱码亚洲电影| 日韩电影免费在线观看网址 | 青青在线久青草免费观看| 亚洲国产亚洲片在线观看播放| 免费av欧美国产在钱| 亚洲精品无码久久久久秋霞| 国产成人aaa在线视频免费观看| 人人鲁免费播放视频人人香蕉| 亚洲宅男天堂在线观看无病毒 | 91免费播放人人爽人人快乐| 男人天堂2018亚洲男人天堂| 日韩成人免费视频播放| 一本到卡二卡三卡免费高| 国产成人A人亚洲精品无码| h片在线免费观看| 亚洲AV无码AV吞精久久| 亚洲精品WWW久久久久久| 永久免费av无码入口国语片| 4444亚洲国产成人精品| 嫩草影院免费观看| 人人公开免费超级碰碰碰视频 | 日韩免费一区二区三区在线播放| 亚洲欧美一区二区三区日产| 中文字幕亚洲综合久久男男| 99re视频精品全部免费| 国产精品亚洲天堂| 亚洲Av熟妇高潮30p|