Groovy中与某个文件类型匹配的所有文件的递归列表 [英] Recursive listing of all files matching a certain filetype in 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屋!