HOME      BLOG       FORUMS      CONTACT       ABOUT

Writing a filename filter.

Writing a filename filter.

We need to implement the FilenameFilter class for writing a filename filter. The FilenameFilter interface contains only one method accept(File, String). This method is used for checking whether a file should be included.

import java.io.File;
import java.io.FilenameFilter;
/**
 *
 * A file filter.
 *
 */
class FileFilter implements FilenameFilter{
	/**
	 * Pattern would contain the extension of
	 * the file.
	 */
	private String pattern;

	/**
	 * Initializes the pattern.
	 *
	 * Provide the file extension to filter.
	 *
	 * @param pattern
	 */
	public FileFilter(String pattern){
		this.pattern  = pattern;
	}

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

}

Share

Tags: , , , ,


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button

Leave a Reply