[ Back | Previous | Next ]

How to use FilenameFilter?

Package:
java.io.*
Product:
JDK
Release:
1.0.2
Related Links:
General
File
FilenameFilter
FileWriter
JPEGCodec
ObjectInputStream
OutputStream
PipedInputStream
PrintWriter
StreamTokenizer
Comment:
import java.io.FilenameFilter;
import java.io.File;
class HTMLFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".html"));
    }
}
class Main {
    public static void main (String[] args) {
        String dir = ".";
        if (args.length == 1)
            dir = args[0];
       
        File f1 = new File(dir);
        int i;
        String[] ls;
        FilenameFilter filter = new HTMLFilter();
        System.out.println("HTML Files: " );
        for (ls = f1.list(filter), i = 0;
             ls != null && i < ls.length;
             System.out.println("\t" + ls[i++]));
    }
}
1