zoukankan      html  css  js  c++  java
  • FilenameFilter

    Introduction:

    java.io.FileNameFilter is a interface which is for filtering by filename, if filename can meet the standard we set,return list of files.

    usages:

    creating a class and implementing the interface of FileNameFilter,what you gonna do is override the public function of accept.

      Details of accept function:

     boolean accept(File dir, String name);

    Accept is a callback function,and first invoke is in the following functions.

    (1)String []fs = f.list(FilenameFilter filter);

    (2)File[]fs = f.listFiles(FilenameFilter filter);

      public String[] list(FilenameFilter filter) {
            String names[] = list();
            if ((names == null) || (filter == null)) {
                return names;
            }
            List<String> v = new ArrayList<>();
            for (int i = 0 ; i < names.length ; i++) {
                if (filter.accept(this, names[i])) {
                    v.add(names[i]);
                }
            }
            return v.toArray(new String[v.size()]);
        }

     public File[] listFiles(FilenameFilter filter) {
            String ss[] = list();
            if (ss == null) return null;
            ArrayList<File> files = new ArrayList<>();
            for (String s : ss)
                if ((filter == null) || filter.accept(this, s))
                    files.add(new File(s, this));
            return files.toArray(new File[files.size()]);
        }

    Example:

    public class Demo2 {

     public static void main(String[] args) {       

        File f = new File("C:/Users/caich5/Desktop/Chris/note"); 

        MyFilter filter = new MyFilter(".txt");    String[] files = f.list(filter);    

        for(String a:files){     

            System.out.println(a);   

         }   

    }   

    static class MyFilter implements FilenameFilter{   

         private String type;    

         public MyFilter(String type){     

              this.type = type;    

         }    

         public boolean accept(File f,String name){     

         return name.endsWith(type);    

          }  

       }

    }

    Aimer,c'est partager
  • 相关阅读:
    脉络清晰的BP神经网络讲解,赞
    Git工作流指南:Gitflow工作流 Comparing Workflows
    局部敏感哈希Locality Sensitive Hashing(LSH)之随机投影法
    CMake入门指南-编译教程
    LaTeX新人教程,30分钟从完全陌生到基本入门
    Android 高手进阶之自定义View,自定义属性(带进度的圆形进度条)
    JSON 数组
    JSON 对象
    JSON语法
    JSON
  • 原文地址:https://www.cnblogs.com/pickKnow/p/8507618.html
Copyright © 2011-2022 走看看