FileFilter file filter

FileFilter

File[] listFiles(FileFilter filter)
returns an array of abstract pathnames, representing the files and directories in the directory that satisfy the specified filter represented by this abstract pathname.
Insert picture description here

Create an implementation class to implement the FileFilter interface

java.io
Interface FileFilter Abstract pathname filter.
boolean accept(File pathname)
tests whether the specified abstract pathname should be included in the pathname list.
File[] listFiles(FilenameFilter filter)
returns an array of abstract pathnames, representing the files and directories in the directory that satisfy the specified filter represented by this abstract pathname.

Implementation class code:

public class Demo09FilterImpl implements FileFilter {
@Override
public boolean accept(File pathname) {

    if (pathname.isDirectory())

        return true;
    return pathname.toString().endsWith(".java");
}
}

Code meaning: If the file is a folder, it will be returned, and if it is a file with a java suffix, it will also be returned; that is to say, in the test class method, only folders and java files can be selected in the listFiles. ListFiles was introduced in the previous chapter!

Test code:

public class Demo09FileFilter {
public static void main(String[] args) {
    File file = new File("E:\\xpu\\ideaproject\\20190905\\src\\cn\\itcast\\File");
    digui(file);
}
public static void digui(File dir)
{
    File[] files = dir.listFiles(new Demo09FilterImpl());//加载过滤器实现类
    for (File f :
            files) {
        if(f.isDirectory())//判断是否为文件夹,如果是
            digui(f);//使用递归,继续遍历其中的文件
        else {
            String s = f.toString();
            boolean b = s.endsWith(".txt");
            if (b)
                System.out.println(f);
        }


    }
}
}

Effect demonstration:

Process finished with exit code 0
Since the java files and folders are not filtered, and there is no txt text file, there is no print result!

Use anonymous inner classes to implement file filters

Code:

public class Demo10FileFilterannoy {
public static void main(String[] args) {
    File file = new File("E:\\xpu\\ideaproject\\20190905\\src\\cn\\itcast\\File");
    digui(file);
}
public static void digui(File dir)
{
  /*  File[] files = dir.listFiles(new FileFilter() {
        @Override
        public boolean accept(File pathname) {
            if(pathname.isDirectory())
                return true;
            return pathname.toString().endsWith(".java");
        }
    });//加载过滤器实现类*/
    //使用lambda
    /*File[] files = dir.listFiles((pathname)->{
        {
            if(pathname.isDirectory())
                return true;
            return pathname.toString().endsWith(".java");
        }
    });//加载过滤器实现类*/
    //简化lambda
   /* File[] files = dir.listFiles((pathname)->  pathname.isDirectory()||pathname.toString().endsWith(".java")
    );//加载过滤器实现类*/
   //第二种过滤器
    /*File[] files = dir.listFiles(new FilenameFilter() {
        @Override
        public boolean accept(File dir, String name) {
            File pathname=new File(dir,name);
            return pathname.isDirectory()||name.toString().endsWith(".java");
        }
    });*/
    //使用lambda
   /*     File[] files = dir.listFiles((d, name)-> {

            return new File(d,name).isDirectory()||name.toString().endsWith(".java");

    });*/
    //简化lambda
    File[] files = dir.listFiles((d,name)->new File(d,name).isDirectory()||name.toString().endsWith(".java"));
    for (File f :
            files) {
        if(f.isDirectory())//判断是否为文件夹,如果是
            digui(f);//使用递归,继续遍历其中的文件
        else {
            String s = f.toString();
            boolean b = s.endsWith(".java");
            if (b)
                System.out.println(f);
        }

    }
}
}

Among them is the use of lambda to simplify the code: readers are asked to search for other articles

Guess you like

Origin blog.csdn.net/tangshuai96/article/details/102764317