您的位置:首页 > 编程语言 > Java开发

南哥带你玩转 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: