简体   繁体   中英

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. 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.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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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