开发者

What's wrong with this Groovy construct?

This is a short Groovy script:

import org.apache.commons.io.FileUtils;
def dir = new Fil开发者_运维知识库e("/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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜