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

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

问题描述

我有一个具有此结构的文件夹

I have a folder with this structure

mainFolder

mainFolder

   --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?

PS:
当然Settings.getSiemensOptionAWL )只是布尔函数taht返回我的决定

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;
    }
}

然后简单地结合简短的FileFilters ,.scl和.awl的情况。示例FailFastFileFilter我上面显示的将允许你指定null作为过滤器之一(所以你可以使用inline 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看起来像: / p>

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天全站免登陆