java2参考大全上的例子的源码和自己的理解.
源代码在线查看: dirlistonly.java~2~
package filenamefilter接口;
/**
1.txt
2.txt
*/
// Directory of .HTML files.
import java.io.*;
class OnlyExt
implements FilenameFilter {
String ext;
public OnlyExt(String ext) {
this.ext = "." + ext;
}
public boolean accept(File dir, String name) {
return name.endsWith(ext);
}
}
class DirListOnly {
public static void main(String args[]) {
String dirname = "java";
File f1 = new File(dirname);
FilenameFilter only = new OnlyExt("txt");
String s[] = f1.list(only);
for (int i = 0; i < s.length; i++) {
System.out.println(s[i]);
}
}
}