Skip to content

Latest commit

 

History

History
12 lines (10 loc) · 554 Bytes

java-list-files-in-directory-regex.md

File metadata and controls

12 lines (10 loc) · 554 Bytes

Tags: java|java-io|java-regex Title: Java: List all the files in a directory based on a regex

If you define a regular expression with Pattern, then use the matcher() and matches() methods on that within a File's listFiles() method, you will get back a list of the files in the File's directory based on the regex.

final Pattern p = Pattern.compile(regex);
File[] pagesTemplates = file.listFiles(new FileFilter() {
    @Override
    public boolean accept(File f) {
       	return p.matcher(f.getName()).matches();
    }
});