File Filters

The FileFilter interface is very similar to FilenameFilter:

public abstract interface FileFilter

The accept( ) method of FileFilter takes a single File object as an argument, rather than two strings giving the directory and path:

public boolean accept(File pathname)

Example 17-8 is a filter that only passes HTML files. Its logic is essentially the same as the filter of Example 17-7.

Example 17-8. HTMLFileFilter

public 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;

This class appears as an argument in one of the listFiles( ) methods of

public File[] listFiles(FileFilter filter)

Example 17-9 uses the HTMLFileFilter to list the HTML files in the current working directory.

Example 17-9. List HTML files

public class HTMLFiles {
 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++) {

Basic I/O

Introducing I/O

Output Streams

Input Streams

Data Sources

File Streams

Network Streams

Filter Streams

Filter Streams

Print Streams

Data Streams

Streams in Memory

Compressing Streams

JAR Archives

Cryptographic Streams

Object Serialization

New I/O



Nonblocking I/O

The File System

Working with Files

File Dialogs and Choosers


Character Sets and Unicode

Readers and Writers

Formatted I/O with java.text


The Java Communications API


The J2ME Generic Connection Framework


Character Sets

Java I/O
Java I/O
ISBN: 0596527500
EAN: 2147483647
Year: 2004
Pages: 244 © 2008-2020.
If you may any questions please contact us: