这个Groovy构造有什么问题? [英] What's wrong with this Groovy construct?
本文介绍了这个Groovy构造有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是一段简短的Groovy脚本:
import org.apache.commons.io.FileUtils;
def dir = new File(/ mydir)
def files = FileUtils.listFiles(dir,new String [] {java},false)
它表示:数组构造函数调用:2
有什么不对?
解决方案
调用应该是:
def files = FileUtils.listFiles dir,[java] as String [],false)
Groovy默认使用列表 as
操作符可用于将这些列表强制转换为指定类型的数组(通常用于与java api进行交互,如本例中所示)
另外,您可以像纯粹的Groovy一样执行此操作:
def files = dir.listFiles()。findAll {it.name ==〜/.*\.java/}
然后,您不需要Commons FileUtils
This is a short Groovy script:
import org.apache.commons.io.FileUtils;
def dir = new File("/mydir")
def files = FileUtils.listFiles(dir, new String[] { "java" }, false)
It says:
No expression for the array constructor call at line: 2
What's wrong?
解决方案
The call should be:
def files = FileUtils.listFiles(dir, [ "java" ] as String[], false)
Groovy uses Lists by default, and the as
operator can be used to coerce these lists into arrays of a specified type (often for interacting with the java api as in this example)
[edit]
As an aside, you can do this with pure Groovy like so:
def files = dir.listFiles().findAll { it.name ==~ /.*\.java/ }
Then, you don't need Commons FileUtils
这篇关于这个Groovy构造有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文