1,文件过滤器:
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\CORAIL_REG_zhanghouyin .xls, c:\zhanghy\New Folder\CORV1P_PIL_IHM-CAP-T0800.xls, c:\zhanghy\New Folder\CORV1P_PIL_IHM-REF-11.xls, c:\zhanghy\New Folder\DateConverter.java, c:\zhanghy\New Folder\e371575_Timesheet.xls, c:\zhanghy\New Folder\ECW.doc, c:\zhanghy\New Folder\KO1QT01.txt]
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]
结果为:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]
public class MyFileFilter {
//不能传参
public static void main(String[] args) {
File dir = new File("c:/zhanghy");
File[] currentFiles = dir.listFiles(new MyFilter());
System.out.println(Arrays.toString(currentFiles));
}
}
class MyFilter implements FileFilter{
//当前的文件跟文件进行比较,如果返回true,则表示符合要求
public boolean accept(File pathname) {
return true;
}
}
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\CORAIL_REG_zhanghouyin .xls, c:\zhanghy\New Folder\CORV1P_PIL_IHM-CAP-T0800.xls, c:\zhanghy\New Folder\CORV1P_PIL_IHM-REF-11.xls, c:\zhanghy\New Folder\DateConverter.java, c:\zhanghy\New Folder\e371575_Timesheet.xls, c:\zhanghy\New Folder\ECW.doc, c:\zhanghy\New Folder\KO1QT01.txt]
public class MyFileFilter {
public static void main(String[] args) {
File dir = new File("c:/zhanghy/New Folder");
File[] currentFiles = dir.listFiles(new MyFilter());
System.out.println(Arrays.toString(currentFiles));
}
}
class MyFilter implements FileFilter{
//取得所有java文件,(判断当前文件是.java结尾)
public boolean accept(File pathname) {
if(pathname.getName() != null && pathname.getName().endsWith(".java")){
return true;
}
return false;
}
}
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]
public class MyFileFilter {
//可以传参
public static void main(String[] args) {
File dir = new File("c:/zhanghy/New Folder");
//取得所有.java文件
File[] currentFiles = dir.listFiles(new YourFilter(".java"));
System.out.println(Arrays.toString(currentFiles));
}
}
class YourFilter implements FileFilter{
private String requestFile;//过滤条件
public YourFilter(String requestFile){
this.requestFile = requestFile;
}
public boolean accept(File pathname) {
if(pathname.getName() != null && pathname.getName().endsWith(requestFile)){
return true;
}
return false;
}
}
结果为:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]
public class MyFileFilter {
//不能传参
private static FileFilter myFilter = new FileFilter() {
public boolean accept(File pathname) {
if(pathname.getName() != null && pathname.getName().endsWith(".java"))
return true;
return false;
}
};
public static void main(String[] args) {
File dir = new File("c:/zhanghy/New Folder");
//取得所有.java文件
File[] currentFiles = dir.listFiles(myFilter);
System.out.println(Arrays.toString(currentFiles));
}
}
结果:
[c:\zhanghy\New Folder\BooleanConverter.java, c:\zhanghy\New Folder\DateConverter.java]