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 |
public interface FilenameFilter { /** * Test whether the specified file should be included in a file list. * * @ Param: directory of the file found. * @ Param file name */ boolean accept(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 |
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 * 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. */ 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); // 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.