Java复制一个不包括一些内部文件的文件夹 [英] Java copy a folder excluding some internal file

查看:35
本文介绍了Java复制一个不包括一些内部文件的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有这种结构的文件夹

I have a folder with this structure

主文件夹

   --Sub1  
         --File .scl
         --File .awl
         --Other files
   --Sub2  
         --Files
   --Sub3
   --Sub4

我想将它复制到另一个位置,但我希望避免使用 Sub3 并且(根据情况)从 Sub1 中获取一些文件

I want to copy it to another location but i want the Sub3 to be avoided and (depending from the situation) some file from the Sub1

这是我到目前为止所做的摘录:

Here is an extract from what i did so far:

FileUtils.copyDirectory(srcDir, dstDir, new FileFilter() {
        public boolean accept(File pathname) {
            // We don't want 'Sub3' folder to be imported
            // + look at the settings to decide if some format needs to be
            // excluded
            String[] ignoreList= new String[]{
                    !Settings.getSiemensOptionAWL() ? ".awl":"uselessStringWilNeverBeFound",
                    !Settings.getSiemensOptionSCL() ? ".scl":"uselessStringWilNeverBeFound",
                    "Sub3"
            };

            return !(ignoreFile(pathname, ignoreList) && pathname
                    .isDirectory());
        }
    }, true);


    public static boolean ignoreFile(File file, String[] ignoreList) {
        for (final String ignoreStr : ignoreList)
            if (file.getAbsolutePath().contains(ignoreStr))
                return true;
        return false;
    }

显然它接缝工作.但我认为这是一个非常丑陋的解决方案......有谁知道更好的方法吗?

Apparently it seams to work. But i think is a very ugly solution.... Does anyone knows a better way?

附注:当然 Settings.getSiemensOptionAWL() 只是布尔函数,它会返回我的决定

P.S: of course Settings.getSiemensOptionAWL() is just boolean function taht return my decision

推荐答案

这里建议的其他选项都不错,但另一种选择是将多个更简单的 FileFilter 嵌套在一起(当然,这可能有点矫枉过正!)

The other options suggested here are good, however another alternative is to nest multiple simpler FileFilters together (which may be overkill, of course!)

public class FailFastFileFilter implements FileFilter {
    protected final List<FileFilter> children = new ArrayList<FileFilter>();

    public FailFastFileFilter(FileFilter... filters) {
        for (FileFilter filter: filters) {
            if (filter != null)
                this.filters.add(filter);
        }       
    }

    public boolean accept(File pathname) {
        for (FileFilter filter: this.filters) {
            if (!filter.accept(pathname)) {
                return false; // fail on the first reject
            }
        }

        return true;
    }
}

然后为 Sub3 情况、.scl 和 .awl 情况简单地组合简短、简洁的 FileFilter.我上面显示的示例 FailFastFileFilter 将允许您将 null 指定为过滤器之一(因此您可以使用内联 if 语句来确定是否应用了特定的 FileFilters)

Then simply combine short, concise FileFilters for the Sub3 case, the .scl and the .awl case. The example FailFastFileFilter I've shown above would let you specify null as one of the filters (so you could use inline if statements to determine whether particular FileFilters are applied)

为了完整起见,这里是我如何为 Sub1 案例和 Sub3 案例实现子过滤器的一般概念.

For the sake of completion, here's a general idea of how I'd implement the child filters for the Sub1 cases and the Sub3 case.

首先,过滤器以排除目录中具有特定扩展名的文件:

First, a filter to excluding files with a particular extension within a directory:

public class ExcludeExtensionInDirFileFilter implements FileFilter {
    protected final String parentFolder;
    protected final String extension;

    public ExtensionFileFilter(String parentFolder, String extension) {
        this.parentFolder = parentFolder;
        this.extension = extension.toLowerCase();
    }

    public boolean accept(File file) {
        if (!file.isDirectory() && file.getParentFile().getName().equalsIgnoreCase(parentFolder))
            return !file.getAbsolutePath().toLowerCase().endsWith(extension);
        else
            return true;
    }
}

然后排除一个目录:

public class ExcludeDirFileFilter implements FileFilter {
    protected final String name;

    public ExcludeDirFileFilter(String name) {
        this.name = name.toLowerCase();
    }

    public boolean accept(File file) {
        if (file.isDirectory() && file.getName().equalsIgnoreCase(name))
            return false;
        else
            return true;
    }
}

然后设置 FailFastFileFilter 如下所示:

Setting up the FailFastFileFilter would then look something like:

FileFilter filters = new FailFastFileFilter(
    new ExcludeDirFileFilter("Sub3"), // always exclude Sub3
    (!Settings.getSiemensOptionAWL() ? new ExcludeExtensionInDirFileFilter("Sub1",".awl"), null), // Exclude Sub1/*.awl if desired
    (!Settings.getSiemensOptionSCL() ? new ExcludeExtensionInDirFileFilter("Sub1",".scl"), null) // Exclude Sub1/*.scl if desired
);

FileUtils.copyDirectory(srcDir, dstDir, filters);

这篇关于Java复制一个不包括一些内部文件的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆