File-FilenameFilter接口

import java.io.*;
public class Practice_3 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File dir = new File("c:\\新建文件夹\\note");
        
        String[] arr = dir.list(new FilenameFilter()
                {
                    public boolean accept(File dir ,String name)
                    {
                        return true;          //如果返回值为false,则默认地址中无文件, 所以list底层调用了该方法。
                    }
                }
              );
        sop(arr.length);
        for( String s : arr)
        {
            sop(s);
        }
    }
    public static void sop(Object obj)
    {
        System.out.println(obj);
    }

}



过滤器:
import java.io.*;
public class Practice_3 {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File dir = new File("c:\\新建文件夹\\note");
        
        String[] arr = dir.list(new FilenameFilter()
                {
                    public boolean accept(File dir ,String name)
                    {
                        sop("dir::"+dir+"....name::"+name);
                        
                        return name.endsWith(".java");
                    }
                }
                                );
        
        sop(arr.length);
        for( String s : arr)
        {
            sop(s);
        }
    }
    public static void sop(Object obj)
    {
        System.out.println(obj);
        
    }
}

 


 

posted @ 2019-09-25 09:20  蚂蚁雅黑1010  阅读(225)  评论(0编辑  收藏  举报