Start Search Contents Index Links About
SimpleFileFilter.java
/*
 * Author: Havard Rast Blok
 * E-mail: 
 * Web   : www.rememberjava.com
 */

package com.rememberjava.io;

import java.io.File;

/**
 * File filter that accepts only on extension.
 */
public class SimpleFileFilter implements java.io.FileFilter
{
  private String extension;

  /**
   * Construct a SimpleFileFilter which accepts filenames
   * ending with extension.
   */
  public SimpleFileFilterString extension )
  {
    this.extension = extension;
  }
  
  /**
  * Test whether the extension of the pathname is the same as
  * the extension of this SimpleFileFilter.
  * The check ignores case of the extension.
  
  @param pathname The abstract pathname to be tested
  @return true if the extensions match.
  */
  public boolean acceptFile pathname )
  {
    String ext;
    String filename;
    int i;
  
    // Test if variables are not null
    ifpathname != null && extension != null)
    {
      //get the file name and position of the extension
      filename = pathname.getName();
      i = filename.lastIndexOf('.');
      
      //check if the file name contains an extension
      ifi > && i < filename.length() )
      {
        ext = filename.substring(i+1);
        
        //test if there is a match
        ifextension.compareToIgnoreCaseext == )
        {
          return true;
        }
      }
    }
    
    return false;
  }
}


site: Håvard Rast Blok
mail:
updated: 27 July 2007