<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FFileFilter</id>
		<title>Java Tutorial/File/FileFilter - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FFile%2FFileFilter"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/File/FileFilter&amp;action=history"/>
		<updated>2026-04-22T11:11:04Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/File/FileFilter&amp;diff=5332&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/File/FileFilter&amp;diff=5332&amp;oldid=prev"/>
				<updated>2010-06-01T05:19:37Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 05:19, 1 июня 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/File/FileFilter&amp;diff=5331&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/File/FileFilter&amp;diff=5331&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:27Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  A suffix based file filter. ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * JBoss, Home of Professional Open Source&lt;br /&gt;
 * Copyright 2005, JBoss Inc., and individual contributors as indicated&lt;br /&gt;
 * by the @authors tag. See the copyright.txt in the distribution for a&lt;br /&gt;
 * full listing of individual contributors.&lt;br /&gt;
 *&lt;br /&gt;
 * This is free software; you can redistribute it and/or modify it&lt;br /&gt;
 * under the terms of the GNU Lesser General Public License as&lt;br /&gt;
 * published by the Free Software Foundation; either version 2.1 of&lt;br /&gt;
 * the License, or (at your option) any later version.&lt;br /&gt;
 *&lt;br /&gt;
 * This software is distributed in the hope that it will be useful,&lt;br /&gt;
 * but WITHOUT ANY WARRANTY; without even the implied warranty of&lt;br /&gt;
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU&lt;br /&gt;
 * Lesser General Public License for more details.&lt;br /&gt;
 *&lt;br /&gt;
 * You should have received a copy of the GNU Lesser General Public&lt;br /&gt;
 * License along with this software; if not, write to the Free&lt;br /&gt;
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA&lt;br /&gt;
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileFilter;&lt;br /&gt;
/**&lt;br /&gt;
 * A &amp;lt;em&amp;gt;suffix&amp;lt;/em&amp;gt; based file filter.&lt;br /&gt;
 * &lt;br /&gt;
 * @version &amp;lt;tt&amp;gt;$Revision: 1958 $&amp;lt;/tt&amp;gt;&lt;br /&gt;
 * @author &lt;br /&gt;
 */&lt;br /&gt;
public class FileSuffixFilter implements FileFilter {&lt;br /&gt;
  /** A list of suffixes which files must have to be accepted. */&lt;br /&gt;
  protected final String suffixes[];&lt;br /&gt;
  /** Flag to signal that we want to ignore the case. */&lt;br /&gt;
  protected final boolean ignoreCase;&lt;br /&gt;
  /**&lt;br /&gt;
   * Construct a &amp;lt;tt&amp;gt;FileSuffixFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param suffixes&lt;br /&gt;
   *          A list of suffixes which files mut have to be accepted.&lt;br /&gt;
   * @param ignoreCase&lt;br /&gt;
   *          &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; if the filter should be case-insensitive.&lt;br /&gt;
   */&lt;br /&gt;
  public FileSuffixFilter(final String suffixes[], final boolean ignoreCase) {&lt;br /&gt;
    this.ignoreCase = ignoreCase;&lt;br /&gt;
    if (ignoreCase) {&lt;br /&gt;
      this.suffixes = new String[suffixes.length];&lt;br /&gt;
      for (int i = 0; i &amp;lt; suffixes.length; i++) {&lt;br /&gt;
        this.suffixes[i] = suffixes[i].toLowerCase();&lt;br /&gt;
      }&lt;br /&gt;
    } else {&lt;br /&gt;
      this.suffixes = suffixes;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Construct a &amp;lt;tt&amp;gt;FileSuffixFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param suffixes&lt;br /&gt;
   *          A list of suffixes which files mut have to be accepted.&lt;br /&gt;
   */&lt;br /&gt;
  public FileSuffixFilter(final String suffixes[]) {&lt;br /&gt;
    this(suffixes, false);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Construct a &amp;lt;tt&amp;gt;FileSuffixFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param suffix&lt;br /&gt;
   *          The suffix which files must have to be accepted.&lt;br /&gt;
   * @param ignoreCase&lt;br /&gt;
   *          &amp;lt;tt&amp;gt;True&amp;lt;/tt&amp;gt; if the filter should be case-insensitive.&lt;br /&gt;
   */&lt;br /&gt;
  public FileSuffixFilter(final String suffix, final boolean ignoreCase) {&lt;br /&gt;
    this(new String[] { suffix }, ignoreCase);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Construct a case sensitive &amp;lt;tt&amp;gt;FileSuffixFilter&amp;lt;/tt&amp;gt;.&lt;br /&gt;
   * &lt;br /&gt;
   * @param suffix&lt;br /&gt;
   *          The suffix which files must have to be accepted.&lt;br /&gt;
   */&lt;br /&gt;
  public FileSuffixFilter(final String suffix) {&lt;br /&gt;
    this(suffix, false);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Check if a file is acceptible.&lt;br /&gt;
   * &lt;br /&gt;
   * @param file&lt;br /&gt;
   *          The file to check.&lt;br /&gt;
   * @return &amp;lt;tt&amp;gt;true&amp;lt;/tt&amp;gt; if the file is acceptable.&lt;br /&gt;
   */&lt;br /&gt;
  public boolean accept(final File file) {&lt;br /&gt;
    boolean success = false;&lt;br /&gt;
    for (int i = 0; i &amp;lt; suffixes.length &amp;amp;&amp;amp; !success; i++) {&lt;br /&gt;
      if (ignoreCase)&lt;br /&gt;
        success = file.getName().toLowerCase().endsWith(suffixes[i]);&lt;br /&gt;
      else&lt;br /&gt;
        success = file.getName().endsWith(suffixes[i]);&lt;br /&gt;
    }&lt;br /&gt;
    return success;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Create custom File filter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileFilter;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    File cwd = new File(System.getProperty(&amp;quot;user.dir&amp;quot;));&lt;br /&gt;
    File[] htmlFiles = cwd.listFiles(new HTMLFileFilter());&lt;br /&gt;
    for (int i = 0; i &amp;lt; htmlFiles.length; i++) {&lt;br /&gt;
      System.out.println(htmlFiles[i]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class HTMLFileFilter implements FileFilter {&lt;br /&gt;
  public boolean accept(File pathname) {&lt;br /&gt;
    if (pathname.getName().endsWith(&amp;quot;.html&amp;quot;))&lt;br /&gt;
      return true;&lt;br /&gt;
    if (pathname.getName().endsWith(&amp;quot;.htm&amp;quot;))&lt;br /&gt;
      return true;&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Custom File Filter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.util.Enumeration;&lt;br /&gt;
import java.util.Hashtable;&lt;br /&gt;
import javax.swing.filechooser.FileFilter;&lt;br /&gt;
public class CustomFileFilter extends FileFilter {&lt;br /&gt;
  private Hashtable filters = null;&lt;br /&gt;
  private String description = null;&lt;br /&gt;
  private String fullDescription = null;&lt;br /&gt;
  private boolean useExtensionsInDescription = true;&lt;br /&gt;
  public CustomFileFilter() {&lt;br /&gt;
    this.filters = new Hashtable();&lt;br /&gt;
  }&lt;br /&gt;
  public CustomFileFilter(String extension) {&lt;br /&gt;
    this(extension, null);&lt;br /&gt;
  }&lt;br /&gt;
  public CustomFileFilter(String extension, String description) {&lt;br /&gt;
    this();&lt;br /&gt;
    if (extension != null)&lt;br /&gt;
      addExtension(extension);&lt;br /&gt;
    if (description != null)&lt;br /&gt;
      setDescription(description);&lt;br /&gt;
  }&lt;br /&gt;
  public CustomFileFilter(String[] filters) {&lt;br /&gt;
    this(filters, null);&lt;br /&gt;
  }&lt;br /&gt;
  public CustomFileFilter(String[] filters, String description) {&lt;br /&gt;
    this();&lt;br /&gt;
    for (int i = 0; i &amp;lt; filters.length; i++) {&lt;br /&gt;
      addExtension(filters[i]);&lt;br /&gt;
    }&lt;br /&gt;
    if (description != null)&lt;br /&gt;
      setDescription(description);&lt;br /&gt;
  }&lt;br /&gt;
  public boolean accept(File f) {&lt;br /&gt;
    if (f != null) {&lt;br /&gt;
      if (f.isDirectory()) {&lt;br /&gt;
        return true;&lt;br /&gt;
      }&lt;br /&gt;
      String extension = getExtension(f);&lt;br /&gt;
      if (extension != null &amp;amp;&amp;amp; filters.get(getExtension(f)) != null) {&lt;br /&gt;
        return true;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  public String getExtension(File f) {&lt;br /&gt;
    if (f != null) {&lt;br /&gt;
      String filename = f.getName();&lt;br /&gt;
      int i = filename.lastIndexOf(&amp;quot;.&amp;quot;);&lt;br /&gt;
      if (i &amp;gt; 0 &amp;amp;&amp;amp; i &amp;lt; filename.length() - 1) {&lt;br /&gt;
        return filename.substring(i + 1).toLowerCase();&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return null;&lt;br /&gt;
  }&lt;br /&gt;
  public void addExtension(String extension) {&lt;br /&gt;
    if (filters == null) {&lt;br /&gt;
      filters = new Hashtable(5);&lt;br /&gt;
    }&lt;br /&gt;
    filters.put(extension.toLowerCase(), this);&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  public String getDescription() {&lt;br /&gt;
    if (fullDescription == null) {&lt;br /&gt;
      if (description == null || isExtensionListInDescription()) {&lt;br /&gt;
        fullDescription = description == null ? &amp;quot;(&amp;quot; : description + &amp;quot; (&amp;quot;;&lt;br /&gt;
        Enumeration extensions = filters.keys();&lt;br /&gt;
        if (extensions != null) {&lt;br /&gt;
          fullDescription += &amp;quot;.&amp;quot; + (String) extensions.nextElement();&lt;br /&gt;
          while (extensions.hasMoreElements()) {&lt;br /&gt;
            fullDescription += &amp;quot;, &amp;quot; + (String) extensions.nextElement();&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        fullDescription += &amp;quot;)&amp;quot;;&lt;br /&gt;
      } else {&lt;br /&gt;
        fullDescription = description;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return fullDescription;&lt;br /&gt;
  }&lt;br /&gt;
  public void setDescription(String description) {&lt;br /&gt;
    this.description = description;&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  public void setExtensionListInDescription(boolean b) {&lt;br /&gt;
    useExtensionsInDescription = b;&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean isExtensionListInDescription() {&lt;br /&gt;
    return useExtensionsInDescription;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Files filter that filters files by their suffix (or extension) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*&lt;br /&gt;
 * $Id: SuffixFilenameFilter.java,v 1.1.1.1 2005/04/07 18:36:22 pocho Exp $&lt;br /&gt;
 */&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FilenameFilter;&lt;br /&gt;
import javax.swing.filechooser.FileFilter;&lt;br /&gt;
/**&lt;br /&gt;
 * Files filter that filters files by their suffix (or extension).&lt;br /&gt;
 * It can be used as file filter for {@link javax.swing.JFileChooser} or&lt;br /&gt;
 * as file filter for io operations (@link java.io.File#listFiles(java.io.FilenameFilter)}.&lt;br /&gt;
 * &lt;br /&gt;
 * @version $Name:  $ - $Revision: 1.1.1.1 $ - $Date: 2005/04/07 18:36:22 $&lt;br /&gt;
 * TODO Test&lt;br /&gt;
 */&lt;br /&gt;
public class SuffixFilenameFilter extends FileFilter implements FilenameFilter {&lt;br /&gt;
  &lt;br /&gt;
  /** Suffix of files */&lt;br /&gt;
  private String suffix;&lt;br /&gt;
  &lt;br /&gt;
  /** Description of suffix (i.e. .jpg may have description JPG)*/&lt;br /&gt;
  private String description;&lt;br /&gt;
  &lt;br /&gt;
  /** Full description of filter - (i.e. JPG (.jpg) */&lt;br /&gt;
  private String fullDescription;&lt;br /&gt;
  &lt;br /&gt;
  /** Indicates whether to use suffix in brackets after description */&lt;br /&gt;
  private boolean useExtensionInDescription = true;&lt;br /&gt;
  /**&lt;br /&gt;
   * Creates new instance with a given accepted suffix of file names.&lt;br /&gt;
   *&lt;br /&gt;
   * @param suffix used suffix of files (i.e. extension &amp;quot;.xml&amp;quot;)&lt;br /&gt;
   */&lt;br /&gt;
  public SuffixFilenameFilter(String suffix) {&lt;br /&gt;
    this(suffix, null);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Creates new instance with a given accepted suffix of file names.&lt;br /&gt;
   *&lt;br /&gt;
   * @param suffix used suffix of files (i.e. extension &amp;quot;.xml&amp;quot;)&lt;br /&gt;
   */&lt;br /&gt;
  public SuffixFilenameFilter(String suffix, String description) {&lt;br /&gt;
    super();&lt;br /&gt;
    setSuffix(suffix);&lt;br /&gt;
    setDescription(description);&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Indicates whether the name is accepted by this filter or not.&lt;br /&gt;
   * File must ending with given mask of this filter.&lt;br /&gt;
   *&lt;br /&gt;
   * @param dir File&lt;br /&gt;
   * @param name String&lt;br /&gt;
   * @return boolean&lt;br /&gt;
   */&lt;br /&gt;
  public boolean accept(File dir, String name){&lt;br /&gt;
  if (name.lastIndexOf(suffix) &amp;gt; 0)&lt;br /&gt;
      return true;&lt;br /&gt;
    else&lt;br /&gt;
      return false;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean accept(File file) {&lt;br /&gt;
    if (file != null) {&lt;br /&gt;
      if (file.isDirectory() || accept(file.getParentFile(), file.getName()))&lt;br /&gt;
          return true;&lt;br /&gt;
    }&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Returns description of this filter - if {@link #isExtensionInDescription()}&lt;br /&gt;
   * is &amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt; then it creates description in following shape:&amp;lt;br&amp;gt;&lt;br /&gt;
   * &amp;lt;code&amp;gt;description (suffix)&amp;lt;/code&amp;gt;. Otherwise it only returns description.&lt;br /&gt;
   * &lt;br /&gt;
   * @see javax.swing.filechooser.FileFilter#getDescription()&lt;br /&gt;
   */&lt;br /&gt;
  public String getDescription() {&lt;br /&gt;
    if (fullDescription == null &amp;amp;&amp;amp; isExtensionInDescription()) {&lt;br /&gt;
        fullDescription = description==null ? &amp;quot;(&amp;quot; : description + &amp;quot; (&amp;quot;;&lt;br /&gt;
        fullDescription += suffix + &amp;quot;)&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
    return fullDescription;&lt;br /&gt;
  }&lt;br /&gt;
  /**&lt;br /&gt;
   * Sets whether to use suffix in description of this filter.&lt;br /&gt;
   * &lt;br /&gt;
   * @param b&lt;br /&gt;
   */&lt;br /&gt;
  public void setExtensionInDescription(boolean b) {&lt;br /&gt;
    useExtensionInDescription = b;&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean isExtensionInDescription() {&lt;br /&gt;
    return useExtensionInDescription;&lt;br /&gt;
  }&lt;br /&gt;
  public void setSuffix(String suffix) {&lt;br /&gt;
    this.suffix = suffix;&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  public void setDescription(String description) {&lt;br /&gt;
    this.description = description;&lt;br /&gt;
    fullDescription = null;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public String getExtension() {&lt;br /&gt;
    return suffix;&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  public String getSimpleDescription() {&lt;br /&gt;
    return description;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Filter by file ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileFilter;&lt;br /&gt;
import java.io.FilenameFilter;&lt;br /&gt;
public class Main {&lt;br /&gt;
  public static void main(String[] argv) throws Exception {&lt;br /&gt;
    File dir = new File(&amp;quot;c:\\temp&amp;quot;);&lt;br /&gt;
    FileFilter fileFilter = new FileFilter() {&lt;br /&gt;
      public boolean accept(File file) {&lt;br /&gt;
        return file.isDirectory();&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    File[] files = dir.listFiles(fileFilter);&lt;br /&gt;
    &lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Filters files based on the extension (what the filename ends with) ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/****************************************************************&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one   *&lt;br /&gt;
 * or more contributor license agreements.  See the NOTICE file *&lt;br /&gt;
 * distributed with this work for additional information        *&lt;br /&gt;
 * regarding copyright ownership.  The ASF licenses this file   *&lt;br /&gt;
 * to you under the Apache License, Version 2.0 (the            *&lt;br /&gt;
 * &amp;quot;License&amp;quot;); you may not use this file except in compliance   *&lt;br /&gt;
 * with the License.  You may obtain a copy of the License at   *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 *   http://www.apache.org/licenses/LICENSE-2.0                 *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing,   *&lt;br /&gt;
 * software distributed under the License is distributed on an  *&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *&lt;br /&gt;
 * KIND, either express or implied.  See the License for the    *&lt;br /&gt;
 * specific language governing permissions and limitations      *&lt;br /&gt;
 * under the License.                                           *&lt;br /&gt;
 ****************************************************************/&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FilenameFilter;&lt;br /&gt;
/**&lt;br /&gt;
 * This filters files based on the extension (what the filename&lt;br /&gt;
 * ends with). This is used in retrieving all the files of a&lt;br /&gt;
 * particular type.&lt;br /&gt;
 *&lt;br /&gt;
 * Eg., to retrieve and print all &amp;lt;code&amp;gt;*.java&amp;lt;/code&amp;gt; files in the current directory:&lt;br /&gt;
 *&lt;br /&gt;
 * &amp;lt;pre&amp;gt;&lt;br /&gt;
 * File dir = new File(&amp;quot;.&amp;quot;);&lt;br /&gt;
 * String[] files = dir.list( new ExtensionFileFilter( new String[]{&amp;quot;java&amp;quot;} ) );&lt;br /&gt;
 * for (int i=0; i&amp;amp;lt;files.length; i++)&lt;br /&gt;
 * {&lt;br /&gt;
 *     System.out.println(files[i]);&lt;br /&gt;
 * }&lt;br /&gt;
 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
 *&lt;br /&gt;
 * @author  Federico Barbieri &amp;lt;fede@apache.org&amp;gt;&lt;br /&gt;
 * @author Serge Knystautas &amp;lt;sergek@lokitech.ru&amp;gt;&lt;br /&gt;
 * @author Peter Donald&lt;br /&gt;
 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $&lt;br /&gt;
 * @since 4.0&lt;br /&gt;
 */&lt;br /&gt;
public class ExtensionFileFilter&lt;br /&gt;
    implements FilenameFilter&lt;br /&gt;
{&lt;br /&gt;
    private String[] m_extensions;&lt;br /&gt;
    public ExtensionFileFilter( final String[] extensions )&lt;br /&gt;
    {&lt;br /&gt;
        m_extensions = extensions;&lt;br /&gt;
    }&lt;br /&gt;
    public ExtensionFileFilter( final String extension )&lt;br /&gt;
    {&lt;br /&gt;
        m_extensions = new String[]{extension};&lt;br /&gt;
    }&lt;br /&gt;
    public boolean accept( final File file, final String name )&lt;br /&gt;
    {&lt;br /&gt;
        for( int i = 0; i &amp;lt; m_extensions.length; i++ )&lt;br /&gt;
        {&lt;br /&gt;
            if( name.endsWith( m_extensions[ i ] ) )&lt;br /&gt;
            {&lt;br /&gt;
                return true;&lt;br /&gt;
            }&lt;br /&gt;
        }&lt;br /&gt;
        return false;&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Generic File Filter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import javax.swing.filechooser.FileFilter;&lt;br /&gt;
public class GenericFileFilter extends FileFilter {&lt;br /&gt;
  private String[] fileExts;&lt;br /&gt;
  private String description;&lt;br /&gt;
  private String extension;&lt;br /&gt;
  public GenericFileFilter(String[] filesExtsIn, String description) {&lt;br /&gt;
    fileExts = filesExtsIn;&lt;br /&gt;
    this.description = description;&lt;br /&gt;
  }&lt;br /&gt;
  public boolean accept(File f) {&lt;br /&gt;
    if (f.isDirectory()) {&lt;br /&gt;
      return true;&lt;br /&gt;
    }&lt;br /&gt;
    extension = getExtension(f);&lt;br /&gt;
    if (extension != null) {&lt;br /&gt;
      for (int i = 0; i &amp;lt; fileExts.length; i++) {&lt;br /&gt;
        if (check(fileExts[i]))&lt;br /&gt;
          return true;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
  private boolean check(String in) {&lt;br /&gt;
    return extension.equalsIgnoreCase(in);&lt;br /&gt;
  }&lt;br /&gt;
  public String getDescription() {&lt;br /&gt;
    return description;&lt;br /&gt;
  }&lt;br /&gt;
  private String getExtension(File file) {&lt;br /&gt;
    String filename = file.getName();&lt;br /&gt;
    int length = filename.length();&lt;br /&gt;
    int i = filename.lastIndexOf(&amp;quot;.&amp;quot;);&lt;br /&gt;
    if (i &amp;gt; 0 &amp;amp;&amp;amp; i &amp;lt; length - 1)&lt;br /&gt;
      return filename.substring(i + 1).toLowerCase();&lt;br /&gt;
    return null;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Inverted File Filter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/****************************************************************&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one   *&lt;br /&gt;
 * or more contributor license agreements.  See the NOTICE file *&lt;br /&gt;
 * distributed with this work for additional information        *&lt;br /&gt;
 * regarding copyright ownership.  The ASF licenses this file   *&lt;br /&gt;
 * to you under the Apache License, Version 2.0 (the            *&lt;br /&gt;
 * &amp;quot;License&amp;quot;); you may not use this file except in compliance   *&lt;br /&gt;
 * with the License.  You may obtain a copy of the License at   *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 *   http://www.apache.org/licenses/LICENSE-2.0                 *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing,   *&lt;br /&gt;
 * software distributed under the License is distributed on an  *&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *&lt;br /&gt;
 * KIND, either express or implied.  See the License for the    *&lt;br /&gt;
 * specific language governing permissions and limitations      *&lt;br /&gt;
 * under the License.                                           *&lt;br /&gt;
 ****************************************************************/&lt;br /&gt;
&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FilenameFilter;&lt;br /&gt;
/**&lt;br /&gt;
 * This takes a &amp;lt;code&amp;gt;FilenameFilter&amp;lt;code&amp;gt; as input and inverts the selection.&lt;br /&gt;
 * This is used in retrieving files that are not accepted by a filter.&lt;br /&gt;
 *&lt;br /&gt;
 * @author &lt;br /&gt;
 * @version CVS $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $&lt;br /&gt;
 * @since 4.0&lt;br /&gt;
 */&lt;br /&gt;
public class InvertedFileFilter&lt;br /&gt;
    implements FilenameFilter&lt;br /&gt;
{&lt;br /&gt;
    private final FilenameFilter m_originalFilter;&lt;br /&gt;
    public InvertedFileFilter( final FilenameFilter originalFilter )&lt;br /&gt;
    {&lt;br /&gt;
        m_originalFilter = originalFilter;&lt;br /&gt;
    }&lt;br /&gt;
    public boolean accept( final File file, final String name )&lt;br /&gt;
    {&lt;br /&gt;
        return !m_originalFilter.accept( file, name );&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  List file with file filter ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FileFilter;&lt;br /&gt;
public class MainClass {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    File cwd = new File(System.getProperty(&amp;quot;user.dir&amp;quot;));&lt;br /&gt;
    File[] htmlFiles = cwd.listFiles(new HTMLFileFilter());&lt;br /&gt;
    for (int i = 0; i &amp;lt; htmlFiles.length; i++) {&lt;br /&gt;
      System.out.println(htmlFiles[i]);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
class HTMLFileFilter implements FileFilter {&lt;br /&gt;
  public boolean accept(File pathname) {&lt;br /&gt;
    if (pathname.getName().endsWith(&amp;quot;.html&amp;quot;))&lt;br /&gt;
      return true;&lt;br /&gt;
    if (pathname.getName().endsWith(&amp;quot;.htm&amp;quot;))&lt;br /&gt;
      return true;&lt;br /&gt;
    return false;&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  This filter accepts Files that are directories ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/****************************************************************&lt;br /&gt;
 * Licensed to the Apache Software Foundation (ASF) under one   *&lt;br /&gt;
 * or more contributor license agreements.  See the NOTICE file *&lt;br /&gt;
 * distributed with this work for additional information        *&lt;br /&gt;
 * regarding copyright ownership.  The ASF licenses this file   *&lt;br /&gt;
 * to you under the Apache License, Version 2.0 (the            *&lt;br /&gt;
 * &amp;quot;License&amp;quot;); you may not use this file except in compliance   *&lt;br /&gt;
 * with the License.  You may obtain a copy of the License at   *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 *   http://www.apache.org/licenses/LICENSE-2.0                 *&lt;br /&gt;
 *                                                              *&lt;br /&gt;
 * Unless required by applicable law or agreed to in writing,   *&lt;br /&gt;
 * software distributed under the License is distributed on an  *&lt;br /&gt;
 * &amp;quot;AS IS&amp;quot; BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *&lt;br /&gt;
 * KIND, either express or implied.  See the License for the    *&lt;br /&gt;
 * specific language governing permissions and limitations      *&lt;br /&gt;
 * under the License.                                           *&lt;br /&gt;
 ****************************************************************/&lt;br /&gt;
&lt;br /&gt;
import java.io.File;&lt;br /&gt;
import java.io.FilenameFilter;&lt;br /&gt;
/**&lt;br /&gt;
 * This filter accepts &amp;lt;code&amp;gt;File&amp;lt;/code&amp;gt;s that are directories.&lt;br /&gt;
 * Eg., here is how to print out a list of the current directory&amp;quot;s subdirectories:&lt;br /&gt;
 *&lt;br /&gt;
 * &amp;lt;pre&amp;gt;&lt;br /&gt;
 * File dir = new File(&amp;quot;.&amp;quot;);&lt;br /&gt;
 * String[] files = dir.list( new DirectoryFileFilter() );&lt;br /&gt;
 * for ( int i=0; i&amp;amp;lt;files.length; i++ )&lt;br /&gt;
 * {&lt;br /&gt;
 *     System.out.println(files[i]);&lt;br /&gt;
 * }&lt;br /&gt;
 * &amp;lt;/pre&amp;gt;&lt;br /&gt;
 *&lt;br /&gt;
 * @author &lt;br /&gt;
 * @version $Revision: 494012 $ $Date: 2007-01-08 11:23:58 +0100 (Mo, 08 Jan 2007) $&lt;br /&gt;
 * @since 4.0&lt;br /&gt;
 */&lt;br /&gt;
public class DirectoryFileFilter&lt;br /&gt;
    implements FilenameFilter&lt;br /&gt;
{&lt;br /&gt;
    public boolean accept( final File file, final String name )&lt;br /&gt;
    {&lt;br /&gt;
        return new File( file, name ).isDirectory();&lt;br /&gt;
    }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
			</entry>

	</feed>