Graphic Java IO: 2. Source Code of FilenameFilter, iofilenamefilter

Source: Internet
Author: User

Graphic Java IO: 2. Source Code of FilenameFilter, iofilenamefilter

Writer: BYSocket)

Weibo: BYSocket

Bean flap: BYSocket

FaceBook: BYSocket

Twitter: BYSocket

From the previous illustration of Java IO: 1. The File source code did not finish all the File. This time I will talk about FilenameFilter, which is written In the filter file Think In Java:

More specifically, this is an example of a policy mode, because list () implements the basic functions, and provides this policy in the form to improve the algorithms required by list () to provide services.

Java. io. FilenameFilter is a file name filter interface that filters out file name groups that comply with the rules.

I. Source Code of FilenameFilter

I/O UML shows that the FilenameFilter interface is independent and does not have its implementation class. Let's take a look at its source code:

?
123456789 publicinterface FilenameFilter {    /**     * Test whether the specified file should be included in a file list.     *     * @ Param: directory of the file found.     * @ Param file name     */    booleanaccept(File dir, String name);}

JDK1.0 is used to filter file names. You only need to input the corresponding directory and file name in the accept () method.

Deep Analysis: an interface must be implemented to calculate the real implementation in the behavior mode. Therefore, the policy mode is used, involving three roles:

Environment (Context) Role

Abstract Policy (Strategy) Role

Context Strategy Role

The structure is as follows:

The FilenameFiler Interface is the abstract policy role here. It can also be implemented using abstract classes.


Ii. Usage

Use FilenameFiler. Code: (small advertising is required, and the code is in the open-source project java-core-learning. Address: https://github.com/JeffLi1993)

?
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 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 * Use of the Class Name Filter */public class FilenameFilterT {     public static void main(String[] args) {        // IO package path        String dir = "src" + File.separator +                "org" + File.separator +                "javacore" + File.separator +                "io";        File file = new File(dir);        // Create a filter file        MyFilter filter = new MyFilter("y.java");        // Filter        String files[] = file.list(filter);                 // Print        for (String name : files) {            System.err.println(name);        }    }         /**     * The internal class implements the filter file interface.     */    staticclass MyFilter implementsFilenameFilter {                 privateString type;                 publicMyFilter (String type) {            this.type = type;        }         @Override        publicboolean accept(File dir, String name) {            returnname.endsWith(type);// End with Type        }             }}

We use the implementation of internal classes to implement the FilenameFilter Interface. Therefore, when we call the interface method in File list, the input of MyFilter allows the File name rule to be obtained according to what we want.

Right-click Run and you can see the output:


Supplement:

String [] fs = f. list ()

File [] fs = f. listFiles ()

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

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



Iii. Summary

1. The source code is very simple. First, let's look at the data structure and design mode. The reason is clear.

2. Learn things, learn more than 1.1 points. Too deep. That's enough.

3. The Code learned by the bricklayer is on github (synchronized with osc git). Welcome to star, give comments, and make progress together. Address: https://github.com/JeffLi1993

Writer: BYSocket)

Weibo: BYSocket

Bean flap: BYSocket

FaceBook: BYSocket

Twitter: BYSocket

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.