FileUtils.copyDirectory without .SVN

时间:2022-07-06 16:42:27

方法1:列出所有文件逐个筛选:

File selectedFolder = new File(path); // path to folder to list

final IOFileFilter dirs = new IOFileFilter() {
@Override
public boolean accept(File file, String s) {
return file.isDirectory();
} @Override
public boolean accept(File file) {
return (!file.getName().toLowerCase().equalsIgnoreCase(".svn"));
} };
// 2nd argument: TRUE filter, returning all files
// 3rd argument: dirs filter, returning all directories except those named .svn
filesList.addAll(FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE, dirs));

方法2:直接用系统的方法:

filesList.addAll(
FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE,
FileFilterUtils.makeSVNAware(null)));

  

对比一下:方法1比较灵活,可以过滤自定义的其他目录如(.XXX),而方法2智能过滤.svn ,类似的方法还有makeCVSAware,这些都是Apache的io操作包

org.apache.commons.io中FileFilterUtils的方法,特别方便。读者可自行google之看文档:http://commons.apache.org/proper/commons-io/javadocs/api-1.4/org/apache/commons/io/filefilter/FileFilterUtils.html

我工程中的方法类似这样:
public void copyFolder(String oldPath, String newPath) throws IOException {
File srcDir = new File(oldPath);
File destDir = new File(newPath);
FileUtils.copyDirectory(srcDir, destDir,
FileFilterUtils.makeSVNAware(null));
}

  老师终于不用担心我的IO操作了,用过它之后,我和我的小伙伴们都惊呆了。开源万岁。


问题答案共享自:http://*.com/questions/11885070/issue-using-iofilefilter 在此谢过广大网友。