图解 Java IO : 二、FilenameFilter源码

来源:互联网 发布:我的危险妻子 知乎 编辑:程序博客网 时间:2024/04/23 21:09

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

微         博:BYSocket

豆         瓣:BYSocket

FaceBook:BYSocket

Twitter    :BYSocket

从上一篇 图解 Java IO : 一、File源码 并没有把所有File的东西讲完。这次讲讲FilenameFilter,关于过滤器文件《Think In Java》中写道:

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

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

一、FilenameFilter源码

image

从IO的UML可以看出,FilenameFilter接口独立,而且没有它的实现类。下面就看看它的源码:

?
1
2
3
4
5
6
7
8
9
publicinterface FilenameFilter {
    /**
     * 测试指定文件是否应该包含在某一文件列表中。
     *
     * @param   被找到的文件所在的目录。
     * @param   文件的名称
     */
    booleanaccept(File dir, String name);
}

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

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

环境(Context)角色

抽象策略(Strategy)角色

具体策略(Context Strategy)角色

结构图如下:

filenameFilter

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


二、使用方法

image

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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);
        }
    }
     
    /**
     *  内部类实现过滤器文件接口
     */
    staticclass MyFilter implementsFilenameFilter {
         
        privateString type;
         
        publicMyFilter (String type) {
            this.type = type;
        }
 
        @Override
        publicboolean accept(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



三、总结

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

2、学东西,学一点一点深一点。太深不好,一点就够了

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

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

微         博:BYSocket

豆         瓣:BYSocket

FaceBook:BYSocket

Twitter    :BYSocket

1 0
原创粉丝点击