//: DirList3.java
// Building the anonymous inner class "in-place"
import java.io.*;public class DirList3 {
public static void main(final String[] args) {
try {
File path = new File(".");
String[] list;
if(args.length == 0)
list = path.list();
else
list = path.list(
new FilenameFilter() {
public boolean
accept(File dir, String n) {
String f = new File(n).getName();
return f.indexOf(args[0]) != -1;
}
});
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
// Building the anonymous inner class "in-place"
import java.io.*;public class DirList3 {
public static void main(final String[] args) {
try {
File path = new File(".");
String[] list;
if(args.length == 0)
list = path.list();
else
list = path.list(
new FilenameFilter() {
public boolean
accept(File dir, String n) {
String f = new File(n).getName();
return f.indexOf(args[0]) != -1;
}
});
for(int i = 0; i < list.length; i++)
System.out.println(list[i]);
} catch(Exception e) {
e.printStackTrace();
}
}
} ///:~
public class test {
public static void main(String[] args) {
File bb = new File("指定的目录");
File[] files = bb.listFiles(new FileFilter(){
public boolean accept(File pathname) {
int index = pathname.getName().lastIndexOf(".");
if ("java".equals(pathname.getName().substring(index + 1))) {
return true;
}
return false;}});
}
}
pathname是要过滤的文件,也可以是一个字符串。