简体   繁体   English

将闭包传递给Groovy中的函数时出错

[英]Error passing closure to function in Groovy

I have written a closure within a Groovy script to copy files, and then I pass that closure to eachFileMatch(regex, closure) to copy all files that match the given regex. 我已经在Groovy脚本中编写了一个闭包来复制文件,然后将该闭包传递给eachFileMatch(regex,closure)以复制与给定regex匹配的所有文件。 When I prototyped it in the Groovy Console everything worked just fine but now when I run it in Eclipse I get the following error: 当我在Groovy Console中对它进行原型设计时,一切工作都很好,但是现在当我在Eclipse中运行它时,出现以下错误:

groovy.lang.MissingMethodException: No signature of method: java.lang.String.eachFileMatch() is applicable for argument types: (java.util.regex.Pattern, file_copy$_run_closure3)

Here is the closure and the call to eachFileMatch() 这是闭包和对eachFileMatch()的调用

def fileCopyClosure = {

if(it.canRead()){
    def destFolder = new File("${outputDirectory}")
    if(!destFolder.exists()){
        println "Creating directory"
        destFolder.mkdir()
    }


    def desti = new File("${outputDirectory}\\${it.name}")
    output = desti.newOutputStream()
    it.eachByte(1024, write)
    output.close()

}
}
sourceDir.eachFileMatch(regex, fileCopyClosure)

尝试new File(sourceDir).eachFileMatch(regex, fileCopyClosure)

从异常, sourceDir是一个字符串,而不是一个File ,你需要eachFileRecurse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM