user225017
user225017

Reputation:

Remove files using a unix-like pattern

I would like to implement something equivalent to the unix command 'rm foo*' using Java. Obviously, I want it to be multi platform. I know that this can be done using the FilenameFilter class and File.delete() method, but I was wondering if I can perform this simple operation in a less verbose way.

Thanks in advance.

Upvotes: 2

Views: 443

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96444

Look in commons-io for org.apache.commons.io.filefilter.WildcardFileFilter.

for (File file : new File(".").listFiles(new WildcardFilter("foo*"))) {
  file.delete();
}

Upvotes: 1

Related Questions