tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Commons IO > Filters > Prefix File Filter

Prefix File Filter 

Apache Commons IO is a java library with simple IO utilities and filters. This requires the library commons-io-2.1.jar to be in classpath. The following example shows using FileFilterUtils.prefixFileFilter() API. It returns a filter that returns TRUE if the filename starts with the specified prefix.


File Name  :  
com/bethecoder/tutorials/commons_io/tests/filters/PrefixFileFilterTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.commons_io.tests.filters;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.PrefixFileFilter;

public class PrefixFileFilterTest {

  /**
   @param args
   @throws IOException 
   */
  public static void main(String[] args) {
    File directory = new File("C:/input");
    
    String[] files = directory.list(FileFilterUtils.prefixFileFilter("t"));
     for (String file : files) {
         System.out.println(file);
     }
     
     System.out.println();
     files = directory.list(new PrefixFileFilter(new String [] {
         "t""re""wri"
     }));
     
     for (String file : files) {
         System.out.println(file);
     }
  }
}
   

It gives the following output,
three.css
two.css

readonly_file.txt
three.css
two.css
writable_file.txt



 
  


  
bl  br