Filtering only for certain file can be done by implementing the FilenameFilter.
1 class FileFilter implements FilenameFilter {
2 public boolean accept(File f, String filename) {
3 return filename.endsWith("txt")
4 }
5 }
6 println new File("directory name").list(new FileFilter())
The shorter more groovy version would be.
1 println new File("directory name").list({d, f-> f ==~ /.*.txt/ } as FilenameFilter)