Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Groovy/Java-发布路径中带有括号的复制文件_Java_Regex_Groovy - Fatal编程技术网

Groovy/Java-发布路径中带有括号的复制文件

Groovy/Java-发布路径中带有括号的复制文件,java,regex,groovy,Java,Regex,Groovy,我目前有一个脚本,通过正则表达式包含和排除将文件从源目录复制到目标目录,但是当路径包含括号时,文件将不会复制 我最初的想法是,问题在于如何读取源和目标,因为(是一个特殊的字符,我试图用转义()替换(但我可能做得不对) import groovy.io.FileType import java.nio.file.* String Source = 'C:/temp/file(s)' String Target = 'C:/newTemp/file(s)' String

我目前有一个脚本,通过正则表达式包含和排除将文件从源目录复制到目标目录,但是当路径包含括号时,文件将不会复制

我最初的想法是,问题在于如何读取源和目标,因为(是一个特殊的字符,我试图用转义()替换(但我可能做得不对)

import groovy.io.FileType
import java.nio.file.*

    String Source = 'C:/temp/file(s)'
    String Target = 'C:/newTemp/file(s)'

    String InclusionsRegexes = "garbage.txt"

    String ExclusionsRegexes = ""

    class RegexInfo
    {
        private String AllRegexes = "";

        public RegexInfo(String RegexString, String RegexType, String Source)
        {
            if(RegexString != null)
            {
                def RegexArray = RegexString.split(",");

                for(item in RegexArray)
                {
                    String fullRegexPath = Source + "/" + item;

                    if(AllRegexes != null && !AllRegexes.isAllWhitespace())
                    {
                        //Add regex value for Or
                        AllRegexes += "|";
                    }
                    AllRegexes += fullRegexPath;
                }
            }
        }
        public String getAllRegexes() { return this.AllRegexes; }
    }

    IncludesRegexInfo = new RegexInfo(InclusionsRegexes, "inclusion", Source);
    ExcludesRegexInfo = new RegexInfo(ExclusionsRegexes, "exclusion", Source);

    File SourceDirToCopy = new File(Source);

    SourceDirToCopy.eachFileRecurse()
    {
        SourceFile ->

        String SourceFilePath = SourceFile.toString().replaceAll("\\\\","/");

        if(SourceFile.isDirectory())
        {
            SourceFilePath += "/"
        }

        if(SourceFilePath.matches(IncludesRegexInfo.getAllRegexes()) && !SourceFilePath.matches(ExcludesRegexInfo.getAllRegexes()))
        {
            File TargetFile = new File(SourceFilePath.replace(Source, Target))
            String TargetFilePath = TargetFile.toString().replaceAll("\\\\", "/");

                if(!TargetFile.getParentFile().exists())
                {
                    TargetFile.getParentFile().mkdirs()
                }
                Files.copy(SourceFile.toPath(), TargetFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
            }
        }
    }

我收到的错误要么是意外字符,要么是文件没有正确移动。

为了解决脚本不复制的问题,在“匹配”时,
if(SourceFilePath.matches(IncludesRegexInfo.getAllRegexes()&&!SourceFilePath.matches(excludesregxinfo.getAllRegexes())
两条线


但是,读取为相同行的字符串在正则表达式方面不匹配。若要解决此问题,必须在
ExcludesRegexInfo.getAllRegexes()中转义
。这是通过
.replaceAll(“\\”,“\\\(”),“\\\\”.replaceAll(“\\”,“\\”)完成的

为了解决脚本不复制的问题,在“匹配”两个字符串时,
if(SourceFilePath.matches(includeRegeInfo.getAllRegexes()&&!SourceFilePath.matches(ExcludesRegexInfo.getAllRegexes())
出现问题


但是,读取为相同行的字符串在正则表达式方面不匹配。若要解决此问题,必须在
ExcludesRegexInfo.getAllRegexes()中转义
。这是通过
.replaceAll(“\\”,“\\\(”),“\\\\”.replaceAll(“\\”,“\\”)完成的

按原样运行脚本时,没有错误消息。该文件根本不复制。我已经对其进行了更多的研究,它似乎出现在“匹配”路径时的
SourceDirToCopy.eachFileRecurse()
中。尽管println应该包含regexinfo.getAllRegexes()
SourceFilePath
在使用
SourceFilePath.matches(includeRegexInfo.getAllRegexes)
时将显示相同的值。表达式的计算结果将为false。就在
文件之前。复制
放置以下断言:
断言SourceFile.exists()
断言TargetFile.getParentFile().exists()
。如果目标文件夹的源文件不存在,您将看到错误。错误
意外字符
与错误的正则表达式连接。是的,但问题是它从未进入
if(SourceFilePath.matches)(IncludesRegexInfo.getAllRegexes()&&!SourceFilePath.matches(excludesregxinfo.getAllRegexes())
来做断言。如果你对
源文件路径
println
includeRegeInfo.getAllRegexes()
你会发现这两个值是相同的。但是我认为既然
是regex中用于分组的特殊字符,它在某种程度上影响了列出的两个变量中的任何一个。那么,您的问题是什么?您需要regex的帮助吗?我想是的,或者至少如何替换该特殊字符
的转义版本。我已尝试用以下代码行替换它
SourceFilePath.replaceAll(“\\(”,Matcher.quoteReplacement(“(”))
但我不确定是否正确选择了要替换的内容。按原样运行脚本时,没有错误消息。该文件根本不复制。我已对其进行了进一步研究,在“匹配”时,它似乎出现在
SourceDirToCopy.eachFileRecurse()中路径。虽然println应该包含
regexinfo.getAllRegexes()
SourceFilePath
的值,但在使用
SourceFilePath.matches(IncludesRegexInfo.getAllRegexes)时,会显示相同的值
表达式的计算结果将为false。就在
文件之前。复制
放入以下断言:
断言SourceFile.exists()
断言TargetFile.getParentFile().exists()
。如果目标文件夹的源文件不存在,您将看到错误。错误
意外字符
与错误的正则表达式连接。是的,但问题是它从未进入
if(SourceFilePath.matches)(IncludesRegexInfo.getAllRegexes()&&!SourceFilePath.matches(excludesregxinfo.getAllRegexes())
来做断言。如果你对
源文件路径
println
includeRegeInfo.getAllRegexes()
你会发现这两个值是相同的。但是我认为既然
是regex中用于分组的特殊字符,它在某种程度上影响了列出的两个变量中的任何一个。那么,您的问题是什么?您需要regex的帮助吗?我想是的,或者至少如何替换该特殊字符
的转义版本。我已尝试用以下代码行替换它
SourceFilePath.replaceAll(\\(“,Matcher.quoteReplacement(”)
,但我不确定是否正确选择了要替换的内容。