Home  >  Article  >  Java  >  Summary of the use of Java IO file filters

Summary of the use of Java IO file filters

零下一度
零下一度Original
2017-06-17 13:55:591727browse

java io flow uses a lot of design patterns, the most typical one is decoration mode, and command mode, the following is divided into two parts to talk about Java IO files Filter Let’s take a look at the use of the command design pattern

1. The finishing touch

Java io flow is used a lot The most typical design modes are decoration mode and command mode. Through a simple use of the file filter function, you can get a glimpse of the typical use of the command design pattern by Java IO streams. The following is divided into two parts. First, I will give an example of the simple use of the file filter, and then extend it to how it uses the command design pattern. I will not talk about the pattern in great length. If you want to have a deeper understanding of the command pattern, please refer to other articles. material.

2. Example

When we do javaprogramming, if we want to access a file or directory, we will use File class, File is a very powerful class that can be used regardless of files or directories. If you want to traverse files, you will use the list() method of the File class. It will list all sub-file names and path names of the current File object. What if some file names or path names do not want to be listed? The File class provides Another overloaded method list (FilenameFilter filter) with filtering function can meet our needs. The method is declared as follows:

public String[] list(FilenameFilter filter);

The FilenameFilter in the parameter is the file name filtering interface, which has a method accept() method, our custom file name filter needs to implement this interface and implement the accept() method to customize the rules. The FilenameFilter interface declaration is as follows:

public interface FilenameFilter {
 boolean accept(File dir, String name);
}

Let’s look at how to use it. It means to list all file names or path names ending with .xml in the current workspace.

1. Write a custom file filter:

package org.light4j.io.fileFilter;
import java.io.File;
import java.io.FilenameFilter;
/**
 * 自定义文件名过滤器
 * 
 * @author longjiazuo
 * 
 */
public class MyFilenameFilter implements FilenameFilter {
 @Override
 public boolean accept(File dir, String name) {
  // 如果文件以.xml结尾则符合条件返回true
  return name.endsWith(".xml");
 }
}

2. Write a test class

package org.light4j.io.fileFilter;
import java.io.File;
/**
 * 文件过滤器测试
 * 
 * 

* 列出当前工作目录下符合条件的所有子文件名和路径名 *

* * @author longjiazuo * */ public class FilenameFilterTest { public static void main(String[] args) { // 获取工作目录 String workDir = System.getProperty("user.dir"); File file = new File(workDir); // 列举当前工作目录下的所有文件 String[] files = file.list(new MyFilenameFilter()); // 循环遍历 for (String string : files) { System.out.println(string); } } }

You can run the above code In the console, only file names or path names ending with .xml are listed.

3. Command design pattern

The command pattern is about how to handle an object requesting another object to call its method to complete a certain function. mode, when an object requests another object to call its method, it does not interact directly with the requested object, but encapsulates the request into a command object. The specific method is to encapsulate the request in a method of the command object. , passing the command object as a parameter. The core of the command pattern is to use command objects to encapsulate method calls.

In a project, we will not have only one class, so it often involves one object requesting another object to call its method to achieve a certain purpose. If the requester does not want or cannot directly interact with the requestee at this time, in other words, the requester does not contain the reference of the requestee, then how do the two interact at this time? We can use the command model to achieve the goal. For example, when we order takeout every day, we do not directly deal with the restaurant, but go to the takeout website such as Meituan to purchase. The takeout website will tell the restaurant your request to buy takeout as an ordering order, as long as the ordering order is successful. Pass it to the restaurant and you'll be sure to receive takeout.

Now back to the use of file filters, the list (FilenameFilter filter) method of the File class implements the function of filtering file names by using an interface object as a parameter. The specific rule definition is in the FilenameFilter interface This is implemented in the accept() method of the subclass. This is the use of the command design pattern.

Logically speaking, since the list(FilenameFilter filter) method wants a rule to determine which files need to be listed, then we can just pass the rule in. This rule is a code block, but the current Java does not It does not support passing code blocks into the method, so Java uses the accept() method of FilenameFilter to encapsulate the judgment rules. The function of the passed-in MyFilenameFilter object is actually to pass in the method body of the accept() method. This method body The function is to determine which file names or path names comply with the rules. The implementation source code of list(FilenameFilter filter) is as follows:

The above is the detailed content of Summary of the use of Java IO file filters. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn