Schematic Java IO: FilenameFilter Source code

Source: Internet
Author: User

Writer:bysocket (mud and brick pulp carpenter)

Micro-Blog: Bysocket

Watercress: Bysocket

FaceBook: Bysocket

Twitter: Bysocket

from the previous article diagram Java IO: First, File source code did not finish all the file things. This time about FilenameFilter, the filter file Think in Java wrote:

More specifically, this is an example of a strategy pattern, because list () implements the basic functionality and provides this strategy in the form of a refinement of the algorithm that the list () needs to provide the service.

Java.io.FilenameFilter is the file name filter interface, which filters out the group of file names that match the rule.

First, FilenameFilter source

As can be seen from the UML of Io, thefilenamefilter interface is independent and does not have its implementation class . Here's a look at its source code:

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

From the JDK1.0 there is a simple function: to filter the file name . Just pass in the appropriate directory and file name in the Accept () method.

in-depth analysis: The interface must have a real implementation to calculate the real implementation in the behavior pattern. So here's the strategy model , which involves three roles:

Environment (context) role

abstract Policy (strategy) role

specific policy (Context strategy) role

The structure diagram is as follows:

Among them,Filenamefiler Interface is the abstract policy role here. In fact, it can also be implemented with abstract classes .

Ii. Methods of Use

Filenamefiler use. On the Code: (Small advertising is to be, the code is in the open source project java-core-learning. address :https://github.com/JeffLi1993)

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 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 * *   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);        }    }        /**     *  内部类实现过滤器文件接口     */    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结尾        }            }}

We implement the FilenameFilter Interface with the implementation of the inner class . So when our file list calls the interface method, the incoming myfilter can let the file name rule be obtained as we want.

Right-click Run, you can see the output:

Add:

string[] fs = F.list ()

file[] fs = F.listfiles ()

String []FS = F.list (filenamefilter filter);;

File[]fs = F.listfiles (filenamefilter filter);

Iii. Summary

1, look at the source code is very simple, see how to use First, in-depth look at what data structure, design patterns. Taken care is clear.

2, learn something, learn 1.1 deep point. It's too deep to be good enough.

3, Masons learn the code are on GitHub (synchronous osc git), welcome to Point Star, suggestions, progress together. Address: https://github.com/JeffLi1993

Writer:bysocket (mud and brick pulp carpenter)

Micro-Blog: Bysocket

Watercress: Bysocket

FaceBook: Bysocket

Twitter: Bysocket

Schematic Java IO: FilenameFilter Source code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.