首页 > 解决方案 > 使用 eachFileRecurse 递归列出 Groovy 中的所有文件返回 FileNotFoundException

问题描述

我的new File工作并包含任何 xml 文件,但我的但eachFileRecurse返回java.io.FileNotFoundException

/home/jenkins/workspace/myjob/abcd/TestFlux
Caught: java.io.FileNotFoundException: /home/jenkins/workspace/myjob/abcd/TestFlux
java.io.FileNotFoundException: /home/jenkins/workspace/myjob/abcd/TestFlux
    at tests_flux.foo(tests_flux.groovy:48)
    at tests_flux$traiter.callCurrent(Unknown Source)
    at tests_flux.run(tests_flux.groovy:114)

这是我在 Jenkins 上运行的代码:

导入静态 groovy.io.FileType.FILES

def foo() {
    File f = new File("abcd/TestFlux")
    String currentDir = f.getAbsolutePath()
    println currentDir

    f.eachFileRecurse(FILES) {
        println it
    }
}

标签: jenkinsgroovy

解决方案


new File()并不意味着文件存在或任何东西。它是一个持有人,被要求为您查看文件系统。例如

groovy:000> new File("/hehe/lol")
===> /hehe/lol
groovy:000> new File("/hehe/lol").exists()
===> false
groovy:000> new File("/hehe/lol").isDirectory()
===> false

推荐阅读