Java Example of org.apache.commons.io.filefilter

Q

Where can I find an example Java code that uses the org.apache.commons.io.filefilter package?

✍: FYIcenter.com

A

org.apache.commons.io.filefilter package defines an interface (IOFileFilter) that combines both java.io.FileFilter and java.io.FilenameFilter. Besides that the package offers a series of ready-to-use implementations of the IOFileFilter interface including implementation that allow you to combine other such filters. These filters can be used to list files or in FileDialog, for example.

Here is very simple Java example that uses the org.apache.commons.io.filefilter package:

import java.io.File;
import java.io.FileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;

public class WildcardFileFilterTest {
   public static void main(String[] args) throws Exception {
      File dir = new File(".");
      FileFilter fileFilter = new WildcardFileFilter("*.java");
      File[] files = dir.listFiles(fileFilter);
      for (int i = 0; i < files.length; i++) {
         System.out.println(files[i]);
      }
   }
}

You can compile and run the above example in a command window as shown below:

C:\fyicenter>javac -cp C:\local\commons-io-2.5\commons-io-2.5.jar 
   WildcardFileFilterTest.java

C:\fyicenter>java -cp .;C:\local\commons-io-2.5\commons-io-2.5.jar 
   WildcardFileFilterTest
   
.\LastModFileComparatorExample.java
.\SimpleTestMonitor.java
.\WildcardFileFilterTest.java

 

Using commons-io.jar in Java Code

⇒⇒FAQ for Apache commons-io.jar

2017-04-28, 316👍, 0💬