java核心编程之文件过滤类FileFilter和FilenameFilter
本文实例为大家分享了文件过滤类FileFilter和FilenameFilter,供大家参考,具体内容如下
1.FileFilter
packagetest; importjava.io.File; importjava.io.FileFilter; publicclassFileFilterTestimplementsFileFilter{ privateStringextension; publicFileFilterTest(Stringextension){ this.extension=extension; } /** * */ @Override publicbooleanaccept(Filepathname){ if(pathname.isDirectory()){ returnfalse; } Stringname=pathname.getName(); intnum=name.lastIndexOf("."); if(num==-1){ returnfalse; } if(name.length()-1==num){ returnfalse; }else{ returnextension.equals(name.substring(num+1)); } } publicstaticvoidmain(String[]args){ Filepath=newFile("E:\\书籍\\java编程"); //获得过滤对象 FileFilterTestft=newFileFilterTest("pdf"); File[]files=path.listFiles(ft); for(inti=0;i2.FilenameFilter
packagetest; importjava.io.File; importjava.io.FilenameFilter; publicclassFileNameFilterTestimplementsFilenameFilter{ //判断图片是否格式是否是gif privatebooleanisGif(StringfileName){ if(fileName.toLowerCase().endsWith(".gif")){ returntrue; }else{ returnfalse; } } //判断图片是否格式是否是jpg privatebooleanisJpg(StringfileName){ if(fileName.toLowerCase().endsWith(".jpg")){ returntrue; }else{ returnfalse; } } //判断图片是否格式是否是png privatebooleanisPng(StringfileName){ if(fileName.toLowerCase().endsWith(".png")){ returntrue; }else{ returnfalse; } } @Override publicbooleanaccept(Filedir,Stringname){ return(isGif(name)||isJpg(name)||isPng(name)); } publicstaticvoidmain(String[]args){ Filepath=newFile("C:\\img"); //获得过滤对象 FileNameFilterTestft=newFileNameFilterTest(); File[]f=path.listFiles(ft); for(inti=0;i以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。