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:

Publicinterfacefilenamefilter {    /**     * Tests whether the specified file should be included in a list of files.     *     * @param the   directory where the found files are located.     *   Name of the @param file *    /booleanaccept (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)

Packageorg.javacore.io; importjava.io.file;importjava.io.filenamefilter;/* * Copyright [] [Jeff Lee] * * Licensed under the Apache License, Ve Rsion 2.0 (the "License"); * You are not a use this file except in compliance with the License. * Obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * unless required by applicab Le law or agreed into writing, software * Distributed under the License is distributed on a "as is" BASIS, * without WAR Ranties 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 * Class name filter Use */public class Filenamefiltert {public static void M                Ain (string[] args) {//IO packet path String dir = "src" + file.separator + "org" + File.separator +        "Javacore" + file.separator + "IO";        File File = new file (dir);Create Filter file Myfilter filter = new Myfilter ("Y.java");                 Filter String files[] = file.list (filter);        Print for (String name:files) {System.err.println (name); }}/** * Internal class implements filter file interface */staticclassmyfilter implementsfilenamefilter {privatestr                 ing type;        Publicmyfilter (String type) {this.type = type;        } @Override publicbooleanaccept (File dir, String name) {returnname.endswith (type);//End With 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.