Java 之 Java 文件过滤器

文件过滤器

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值