only display files that use the .html extension. : FilenameFilter « File Input Output « Java






only display files that use the .html extension.

   

import java.io.File;
import java.io.FilenameFilter;

class DirListOnly {
  public static void main(String args[]) {
    String dirname = "/java";
    File f1 = new File(dirname);
    FilenameFilter only = new OnlyExt("html");
    String s[] = f1.list(only);

    for (int i = 0; i < s.length; i++) {
      System.out.println(s[i]);
    }
  }
}

class OnlyExt implements FilenameFilter {
  String ext;

  public OnlyExt(String ext) {
    this.ext = "." + ext;
  }

  public boolean accept(File dir, String name) {
    return name.endsWith(ext);
  }
}

   
    
  








Related examples in the same category

1.Removes the file extension from the given file name.
2.Returns the file extension of the given file name. The returned value will contain the dot.
3.Inverted File Filter
4.This filter accepts Files that are directories
5.A simple file filter for a particular file extension
6.Accepts a selection if it is acceptable to both of two FilenameFilters