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

时间:2012-10-18 20:49:12

标签: groovy

我在Groovy脚本中编写了一个用于复制文件的闭包,然后将该闭包传递给eachFileMatch(regex,closure)以复制与给定正则表达式匹配的所有文件。当我在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)

这是闭包和对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)

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

来自例外,sourceDir是一个字符串,而不是File所需的eachFileRecurse