图解 Java IO : 二、FilenameFilter源码

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
public interface FilenameFilter {
     /**
      * 测试指定文件是否应该包含在某一文件列表中。
      *
      * @param   被找到的文件所在的目录。
      * @param   文件的名称
      */
     boolean accept(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
package org.javacore.io;
 
import java.io.File;
import java.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
  *
  *
  * 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);
         }
     }
     
     /**
      *  内部类实现过滤器文件接口
      */
     static class MyFilter  implements FilenameFilter {
         
         private String type;
         
         public MyFilter (String type) {
             this .type = type;
         }
 
         @Override
         public boolean accept(File dir, String name) {
             return name.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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值