Commons Io 简明教程

Apache Commons IO - SuffixFileFilter

SuffixFileFilter 过滤基于后缀的文件。这用于检索特定类型的所有文件。

Class Declaration

以下是 org.apache.commons.io.filefilter.SuffixFileFilter 类的声明 −

public class SuffixFileFilter
   extends AbstractFileFilter implements Serializable

Example of SuffixFileFilter Class

以下是我们需要解析的输入文件 -

Welcome to TutorialsPoint. Simply Easy Learning.

我们打印当前目录中的所有文件和目录,然后过滤扩展名为 txt 的文件。

IOTester.java

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.SuffixFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingSuffixFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingSuffixFileFilter() throws IOException {
      //get the current directory
      File currentDirectory = new File(".");
      //get names of all files and directory in current directory
      String[] files = currentDirectory.list();
      System.out.println("All files and Folders.\n");
      for( int i = 0; i < files.length; i++ ) {
         System.out.println(files[i]);
      }
      System.out.println("\nFile with extenstion txt\n");
      String[] filesNames = currentDirectory.list( new SuffixFileFilter("txt"));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

Output

它将打印以下结果。

All files and Folders.

.classpath
.project
.settings
bin
input.txt
src

File with extenstion txt

input.txt