Groovy中与某个文件类型匹配的所有文件的递归列表 [英] Recursive listing of all files matching a certain filetype in Groovy

查看:296
本文介绍了Groovy中与某个文件类型匹配的所有文件的递归列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图递归列出所有与Groovy中的特定文件类型匹配的文件。 这个例子几乎可以做到。但是,它不会列出根文件夹中的文件。有没有办法将其修改为列出根文件夹中的文件?或者,有没有其他方法可以做到这一点?

I am trying to recursively list all files that match a particular file type in Groovy. This example almost does it. However, it does not list the files in the root folder. Is there a way to modify this to list files in the root folder? Or, is there a different way to do it?

推荐答案

这应该解决您的问题:

import static groovy.io.FileType.FILES

new File('.').eachFileRecurse(FILES) {
    if(it.name.endsWith('.groovy')) {
        println it
    }
}

eachFileRecurse 取一个枚举文件类型,指定您只对文件感兴趣。通过过滤文件的名称可以轻松解决问题的其余部分。值得一提的是, eachFileRecurse 通常在文件和文件夹中递归,而 eachDirRecurse 只能找到文件夹。

eachFileRecurse takes an enum FileType that specifies that you are only interested in files. The rest of the problem is easily solved by filtering on the name of the file. Might be worth mentioning that eachFileRecurse normally recurses over both files and folders while eachDirRecurse only finds folders.

这篇关于Groovy中与某个文件类型匹配的所有文件的递归列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆