3

Can anyone suggest a FileSet package/class in Java. By FileSet I mean a collection of files and directories together with regex-powered inclusion and exclusion rules (similar to Apache Ant). Thanks.

yegor256
  • 102,010
  • 123
  • 446
  • 597

2 Answers2

2

Apache Commons IO FileUtils may be what you want. It has the capability to identify files with an optional filename filter that you can implement yourself.

See the doc for listFiles() or iterateFiles() for more info.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
1

You could make use of File#listFiles() wherein you pass a FileFilter or FilenameFilter where in turn you can specify the desired pattern in the accept() method.

E.g.

File[] txtFiles = file.listFiles(new FilenameFilter() {
    @Override public boolean accept(File dir, String name) {
        return name.endsWith(".txt"); // You can use String#matches() as well.
    }
});
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555