 |
|
|
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 SimpleFileFilter( String 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 accept( File pathname )
{
String ext;
String filename;
int i;
// Test if variables are not null
if( pathname != 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
if( i > 0 && i < filename.length() - 1 )
{
ext = filename.substring(i+1);
//test if there is a match
if( extension.compareToIgnoreCase( ext ) == 0 )
{
return true;
}
}
}
return false;
}
}
|
|
|
 |