使用FileFilter实现对文件的过滤,见代码:
类:
亲测好用!
package com.babyDuncan.File;
import java.io.File;
import java.io.FilenameFilter;
public class testFileFliter {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
File f = new File ("D:/");
MyFielFilter mff = new MyFielFilter();
String [] files = f.list(new MyFielFilter());
for(String s : files){
System.out.println(s);
}
}
}
类:
package com.babyDuncan.File;
import java.io.File;
import java.io.FilenameFilter;
public class MyFielFilter implements FilenameFilter{
@Override
public boolean accept(File dir, String name) {
// TODO Auto-generated method stub
return name.endsWith(".txt");
}
}
亲测好用!