需要commons-io-2.0.1.jar包
package demo.wc.demo2;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.apache.commons.io.DirectoryWalker;
@SuppressWarnings("rawtypes")
public class FileCleaner extends DirectoryWalker {
public FileCleaner() {
super();
System.out.println("-------开始清除-------2");
}
public List<File> clean(File startDirectory) throws IOException {
List<File> results = new ArrayList<File>();
walk(startDirectory, results);
System.out.println("-------开始清除-------3");
return results;
}
@Override
protected void handleStart(File startDirectory, Collection results)
throws IOException {
System.out.println("-------开始清除-------1");
}
@Override
protected void handleEnd(Collection results) throws IOException {
System.out.println("-------结束清除-------11");
}
@Override
protected void handleCancelled(File startDirectory, Collection results,
CancelException cancel) throws IOException {
System.out.println("-------清除异常-------0");
super.handleCancelled(startDirectory, results, cancel);
}
@Override
protected boolean handleIsCancelled(File file, int depth, Collection results)
throws IOException {
// 这里可以设置断点,比如当你找到某个类的时候,停止遍历,默认继续
System.out.println("-------开始清除-------4");
return false;
}
@Override
protected void handleDirectoryStart(File directory, int depth,
Collection results) throws IOException {
System.out.println("****开始处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
System.out.println("-------开始清除-------5");
}
@Override
protected void handleDirectoryEnd(File directory, int depth,
Collection results) throws IOException {
System.out.println("****结束处理:"+directory.getName()+"deep:"+depth+"results:"+results.toString());
System.out.println("-------开始清除-------6");
}
@Override
protected void handleRestricted(File directory, int depth,
Collection results) throws IOException {
System.out.println("****受限制目录:" + directory.getName() + "deep:" + depth
+ "results:" + results.toString());
System.out.println("-------开始清除-------7");
}
/**
* 是否处理某个目录.返回false 不处理
*
* @see 这里直接删除.svn.然后不处理.
*/
@Override
protected boolean handleDirectory(File directory, int depth,
Collection results) {
// delete svn directories and then skip
System.out.println("-------开始清除-------8");
if (".svn".equals(directory.getName())) {
deleteDirectory(directory, results);
return false;
} else {
results.add(directory);// 删除.svn,还有哪些文件夹
return true;
}
}
/**
* 删除文件,并把文件加到删除列表中
*/
@Override
protected void handleFile(File file, int depth, Collection results) {
// delete file and add to list of deleted
// file.delete();
// results.add(file);
// 删除.svn文件后,还有哪些文件
System.out.println("-------开始清除-------9");
}
/**
* 删除目录及目录下的文件夹和文件
*
* @param directory
* @param results
*/
private void deleteDirectory(File directory, Collection results) {
if (directory.isDirectory()) {
File[] list = directory.listFiles();
for (File file : list) {
deleteDirectory(file, results);
}
}
Log(directory.delete(), directory);
results.add(directory);// 删除文件
System.out.println("-------开始清除-------11");
}
/**
* 删除文件或者目录失败日志
*
* @param flag
*/
private void Log(boolean flag, File directory) {
if (!flag) {
System.err.println("删除文件失败:" + directory.getAbsolutePath());
} else {
System.out.println("delete:" + directory.getAbsolutePath());
}
System.out.println("-------开始清除-------12");
}
/**
*
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileCleaner cleaner = new FileCleaner();
//D:\MyWorkingCopy
//D:\\workspace\\branches\\pamirsshop_branches_8-30
File startDirectory = new File("D:\\MyWorkingCopy");
List<File> list = cleaner.clean(startDirectory);
for(File file:list){
// System.out.println("file:"+file.getAbsolutePath());
}
System.out.println("共处理"+list.size()+"个文件");
}
}