You can use File.listFiles(FileFilter) :
public static File[] listFilesMatching(File root, String regex) { if(!root.isDirectory()) { throw new IllegalArgumentException(root+" is no directory."); } final Pattern p = Pattern.compile(regex); // careful: could also throw an exception! return root.listFiles(new FileFilter(){ @Override public boolean accept(File file) { return p.matcher(file.getName()).matches(); } }); }
EDIT
So, to match the files that look like this: TXT-20100505-XXXX.trx where XXXX can be any four consecutive digits, do something like this:
listFilesMatching(new File("/some/path"), "XT-20100505-\\d{4}\\.trx")
Bart kiers
source share