南哥带你玩转 Java 之 Java 文件过滤器
2018-01-30 20:36
295 查看
文件过滤器
public class Demo { public static void main(String[] args) { // 测试过滤器效果 File file = new File("/Users/Desktop/Test"); // 使用重载的 listFile 方法 File[] listFiles = file.listFiles(new MyFileFilter()); for (File file2 : listFiles) { System.out.println(file2.getName()); } } } // 编写自定义的类,实现过滤器接口 class MyFileFilter implements FileFilter{ // 按编写的规则来过滤文件 // 过滤文件时,看这个方法的返回值 // 返回 false 就被过滤 // 返回 true 则被保留 @Override public boolean accept(File pathname) { if (pathname.isDirectory()) { return false; } // System.out.println(pathname); return true; } } /* * .DS_Store * MindNode_5.0.1_xclient.info.dmg * n.txt * Test.txt * www.baidu.com.txt * 汉字转拼音.zip */
/* * 需求:通过过滤器,遍历打印文件夹中所有 txt 文件 */ public class Example { public static void main(String[] args) { File file = new File("/Users/Desktop/Test"); getTxt(file); } public static void getTxt(File file) { File[] listFiles = file.listFiles(new FindTxt()); for (File file2 : listFiles) { if (file2.isFile()) { if (!file2.isHidden()) { System.out.println(file2); } } else { getTxt(file2); } } } } class FindTxt implements FileFilter{ @Override public boolean accept(File pathname) { // 相当于所有第一集的文件都判断了一下,后缀是不是 txt // 文件夹也在其中,被判断,然后过滤掉了 // return pathname.getName().endsWith("txt"); // 判断时文件夹,直接返回 true if (pathname.isDirectory()) { return true; } return pathname.getName().endsWith("txt"); } } /* * /Users/Desktop/Test/n.txt * /Users/Desktop/Test/Test.txt * /Users/Desktop/Test/www.baidu.com.txt */
public class Demo { public static void main(String[] args) { File file = new File("/Users/Desktop/Test"); getTxt(file); } public static void getTxt(File file) { File[] listFiles = file.listFiles(new MyFileNameFilter()); for (File file2 : listFiles) { if (file2.isFile()) { if (!file2.isHidden()) { System.out.println(file2); } } else { getTxt(file2); } } } } // 测试 FilenameFilter 接口 1.0版本的 class MyFileNameFilter implements FilenameFilter{ @Override public boolean accept(File dir, String name) { /* * 参数1:传进来的文件夹根路径 * 参数2:该路径下的文件的名字和文件夹的名字 */ // 利用这两个参数创建一个文件对象出来 File file = new File(dir, name); System.out.println(file); return false; // 返回 false 就过滤,返回 true 就保留 } } /* * /Users/Desktop/Test/n.txt * /Users/Desktop/Test/Test.txt * /Users/Desktop/Test/www.baidu.com.txt */
/* * 通过FilenameFilter过滤器获取指定目录中小于200K的小文件 */ public class Example { public static void main(String[] args) { File file = new File("/Users/Desktop/Test"); getTxt(file); } public static void getTxt(File file) { File[] listFiles = file.listFiles(new FileNameFilter()); for (File file2 : listFiles) { if (file2.isFile()) { if (!file2.isHidden()) { System.out.println(file2); } } else { getTxt(file2); } } } } class FileNameFilter implements FilenameFilter{ @Override public boolean accept(File dir, String name) { File file = new File(dir, name); // 过滤隐藏文件 if (file.isHidden()) { return false; } // 文件夹不过滤 if(file.isDirectory()) { return true; } return file.length() < 200 * 1024; } } /* * /Users/Desktop/Test/n.txt * /Users/Desktop/Test/Test.txt * /Users/Desktop/Test/www.baidu.com.txt */
http://blog.csdn.net/huzongnan/article/list
相关文章推荐
- 初学Java,IO之文件过滤器(四十)
- java文件过滤器(File类listFiles方法可选参数)--获取目录下指定的某类文件
- java JFileChooser与 文件过滤器FileFilter
- Java基础22--IO--文件过滤器
- 自定义java文件过滤器与文件的深度搜索
- java 保存文件对话框的使用以及文件过滤器(FileDialog、JFileChooser)
- 【基础篇】java中输入输出的总括——文件过滤器
- Java_IO流_文件名过滤器及文件过滤器
- java文件的过滤器
- java代码--文件过滤器
- java文件过滤器
- [疯狂Java]I/O:File(文件类,也是文件流的节点)、FilenameFilter(文件过滤器)
- java学习笔记(2):获取文件名和自定义文件过滤器
- JAVA核心技术学习——流与文件(2.组合流过滤器)
- java网络编程--解析通过web.xml文件控制利用过滤器实现编码格式步骤
- Java基础知识强化之IO流笔记10:File类输出指定目录下指定后缀名的文件名称案例(File类的文件过滤器方法改进list( FilenameFilter ff))
- JAVA和C实现文件搜索(递归和文件过滤器)
- java 使用全局过滤器Filter之后 防止css文件与js文件被过滤
- java--FilenameFilter文件过滤器
- java 保存文件对话框的使用以及文件过滤器(FileDialog、JFileChooser)