public class MyFileFilter implements FilenameFilter{
private String s;
public MyFileFilter(String s){
this.s=s;
}
public boolean accept(File dir, String name) {
return name.endsWith(s);
}
}
public class MyFileFilterDemo {
public static void main(String[] args) {
File file = new File("e:/歌曲");
MyFileFilter mf=new MyFileFilter(".mp3");
String[] s =file.list(mf);
for (String string : s) {
//if(mf.accept(file, string)){//为什么这里可以不调用accept方法来判断也OK?
System.out.println(string);
//}
}
}
}