java io 文件

1,文件过滤器:

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值