图解 Java IO : 2、FilenameFilter源码

Writer      :BYSocket(泥沙砖瓦浆木匠) java

微         博:BYSocket git

豆         瓣:BYSocket github

FaceBook:BYSocket 算法

Twitter    :BYSocket express

从上一篇 图解 Java IO : 1、File源码 并无把全部File的东西讲完。此次讲讲FilenameFilter,关于过滤器文件《Think In Java》中写道: apache

更具体地说,这是一个策略模式的例子,由于list()实现了基本功能,而按着形式提供了这个策略,完善list()提供服务所需的算法。 设计模式

java.io.FilenameFilter是文件名过滤器接口,即过滤出符合规则的文件名组。 数据结构

1、FilenameFilter源码

image

从IO的UML能够看出,FilenameFilter接口独立,并且没有它的实现类。下面就看看它的源码: app

publicinterfaceFilenameFilter {
    /**
     * 测试指定文件是否应该包含在某一文件列表中。
     *
     * @param   被找到的文件所在的目录。
     * @param   文件的名称
     */
    booleanaccept(File dir, String name);
}

 

从JDK1.0就存在了,功能也很简单:就是为了过滤文件名。只要在accept()方法中传入相应的目录和文件名便可。 less

深度分析:接口要有真正的实现才能算行为模式中真正实现。因此这里使用的是策略模式,涉及到三个角色:

环境(Context)角色

抽象策略(Strategy)角色

具体策略(Context Strategy)角色

结构图以下:

filenameFilter

其中,FilenameFiler Interface 就是这里的抽象策略角色。其实也能够用抽象类实现。


2、使用方法

image

如图 FilenameFiler使用如图所示。上代码吧:(small 广告是要的,代码都在 开源项目java-core-learning。地址https://github.com/JeffLi1993

packageorg.javacore.io;
 
importjava.io.File;
importjava.io.FilenameFilter;
/*
 * Copyright [2015] [Jeff Lee]
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
/**
 * @author Jeff Lee
 * @since 2015-7-20 13:31:41
 * 类名过滤器的使用
 */
public class FilenameFilterT {
 
    public static void main(String[] args) {
        // IO包路径
        String dir = "src" + File.separator +
                "org" + File.separator +
                "javacore" + File.separator +
                "io";
        File file = new File(dir);
        // 建立过滤器文件
        MyFilter filter = new MyFilter("y.java");
        // 过滤
        String files[] = file.list(filter);
         
        // 打印
        for (String name : files) {
            System.err.println(name);
        }
    }
     
    /**
     *  内部类实现过滤器文件接口
     */
    staticclassMyFilter implementsFilenameFilter {
         
        privateString type;
         
        publicMyFilter (String type) {
            this.type = type;
        }
 
        @Override
        publicbooleanaccept(File dir, String name) {
            returnname.endsWith(type);// 以Type结尾
        }
         
    }
}


其中咱们用内部类的实现,实现了FilenameFilter Interface。因此当咱们File list调用接口方法时,传入MyFilter可让文件名规则按咱们想要的得到。

右键 Run 下,能够看到如图所示的输出:

image


补充:

String[] fs = f.list()

File[] fs = f.listFiles()

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

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

image


3、总结

一、看源码很简单,看怎么用先,在深刻看有什么数据结构,设计模式。理理就清楚了

二、学东西,学一点一点深一点。太深很差,一点就够了

三、泥瓦匠学习的代码都在github上(同步osc git),欢迎你们点star,提意见,一块儿进步。地址:https://github.com/JeffLi1993

Writer      :BYSocket(泥沙砖瓦浆木匠)

微         博:BYSocket

豆         瓣:BYSocket

FaceBook:BYSocket

Twitter    :BYSocket

相关文章
相关标签/搜索