本文实例为大家分享了文件过滤类FileFilter和FilenameFilter,供大家参考,具体内容如下FileFilterpackage test;import java.io.File;import java.io.FileFilte
本文实例为大家分享了文件过滤类FileFilter和FilenameFilter,供大家参考,具体内容如下
FileFilter
package test;import java.io.File;import java.io.FileFilter;public class FileFilterTest implements FileFilter { private String extension; public FileFilterTest(String extension) { this.extension = extension; } @Override public boolean accept(File pathname) { if(pathname.isDirectory()){ return false; } String name = pathname.getName(); int num = name.lastIndexOf("."); if(num == -1){ return false; } if(name.length()-1 == num){ return false; } else { return extension.equals(name.substring(num+1)); } } public static void main(String[] args) { File path = new File("E:\\书籍\\java 编程"); //获得过滤对象 FileFilterTest ft = new FileFilterTest("pdf"); File[] files = path.listFiles(ft); for (int i = 0; i < files.length; i++) { System.out.println(files[i].getAbsolutePath()); } }}
--结束END--
本文标题: java核心编程之文件过滤类FileFilter和FilenameFilter
本文链接: https://lsjlt.com/news/223985.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
2024-05-24
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0