Apache Commons IO - OrFileFilter



OrFileFilter 提供跨文件过滤器列表的条件 OR 逻辑。如果列表中的任一过滤器返回 true,它将返回 true。否则,它将返回 false。

类声明

下面是 org.apache.commons.io.filefilter.OrFileFilter 类的声明 -

public class OrFileFilter
   extends AbstractFileFilter implements ConditionalFileFilter, Serializable

OrFileFilter 类的示例

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

Welcome to TutorialsPoint. Simply Easy Learning.

让我们打印当前目录中的所有文件和目录,然后过滤一个以 . 开头或以 t 结尾的文件。

IOTester.java

import java.io.File;
import java.io.IOException;
import org.apache.commons.io.filefilter.OrFileFilter;
import org.apache.commons.io.filefilter.PrefixFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
public class IOTester {
   public static void main(String[] args) {
      try {
         usingOrFileFilter();
      } catch(IOException e) {
         System.out.println(e.getMessage());
      }
   }
   public static void usingOrFileFilter() 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 starting with . or ends with t\n");
      String[] filesNames = currentDirectory.list( new OrFileFilter(new PrefixFileFilter("."), new WildcardFileFilter("*t")));
      for( int i = 0; i < filesNames.length; i++ ) {
         System.out.println(filesNames[i]);
      }
   }
}

输出

它会打印以下结果。

All files and Folders.

.classpath
.project
.settings
bin
input.txt
src

File starting with . or ends with t

.classpath
.project
.settings
input.txt
广告