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 阅读(3488) 评论(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热点博客

好友博客

搜索

  •  

最新评论

阅读排行榜

评论排行榜