yegor256
yegor256

Reputation: 105053

What's wrong with this Groovy construct?

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?

Upvotes: 17

Views: 8297

Answers (1)

tim_yates
tim_yates

Reputation: 171084

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

Upvotes: 39

Related Questions