Java IO Tutorial - Java FileFilter.accept(File pathname)








Syntax

FileFilter.accept(File pathname) has the following syntax.

boolean accept(File pathname)

Example

In the following code shows how to use FileFilter.accept(File pathname) method.

/*from   w  ww  .  j a  v  a  2s  .  c  o  m*/
import java.io.File;
import java.io.FileFilter;

public class Main {

  public static void main(String[] args) {

    File cwd = new File(System.getProperty("user.dir"));
    File[] htmlFiles = cwd.listFiles(new HTMLFileFilter());
    for (int i = 0; i < htmlFiles.length; i++) {
      System.out.println(htmlFiles[i]);
    }
  }
}

class HTMLFileFilter implements FileFilter {

  public boolean accept(File pathname) {

    if (pathname.getName().endsWith(".html"))
      return true;
    if (pathname.getName().endsWith(".htm"))
      return true;
    return false;
  }
}