List files of a certain type : FileFilter « File Input Output « Java

List files of a certain type



public class Main {

  public static void main(String[] args) {
    File directory = new File("c:\\");

    if (!directory.isDirectory()) {
      System.out.println("No directory provided");

    FilenameFilter filefilter = new FilenameFilter() {
      public boolean accept(File dir, String name) {
        return name.endsWith(".txt");

    String[] filenames = directory.list(filefilter);

    for (String name : filenames) {


Related examples in the same category

1.Filter files by name
2.Filter by file
3.Filters files based on the extension (what the filename ends with)
4.Extension Filename Filter
5.A prefix based filename filter
6.File filter: retrieve at most predefined number of files which are older than specified data
7.A suffix based file filter.
8.A filesystem filter.