java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.io arrow Get the content of a directory with a Filter
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4091
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Get the content of a directory with a Filter E-mail
User Rating: / 7
PoorBest 

First you create a class that implements java.io.FilenameFilter and then code the accept() method, then call File.list() with the filter as a parameter. The returned array of strings has all the names that passed through the accept()filter.

import java.io.File;
import java.io.FilenameFilter;
public class Filter implements FilenameFilter {

  protected String pattern;

  public Filter (String str) {
    pattern = str;
  }

  public boolean accept (File dir, String name) {
    return name.toLowerCase().endsWith(pattern.toLowerCase());
  }

  public static void main (String args[]) {

    if (args.length != 1) {
       System.err.println ("usage: java Filter   ex. java Filter java");
       return;
    }

    Filter nf = new Filter (args[0]);

    // current directory
    File dir = new File (".");
    String[] strs = dir.list(nf);

    for (int i = 0; i < strs.length; i++) {
      System.out.println (strs[i]);
    }
  }
}

Here a version to support multiple filters.

import java.io.File;
import java.io.FilenameFilter;
import java.util.*;
import java.util.TreeSet.*;


public class Filter2 implements FilenameFilter {

  protected Set extensionsSet;

  public Filter2 (String [] extensions) {
    extensionsSet = new TreeSet();

    for (Iterator ext=Arrays.asList(extensions).iterator(); ext.hasNext();) {
      extensionsSet.add(ext.next().toString().toLowerCase().trim());
    }
    extensionsSet.remove("");
  }


  public boolean accept (File dir, String name) {
    final Iterator exts = extensionsSet.iterator();
    while (exts.hasNext()) {
      if (name.toLowerCase().endsWith(exts.next().toString())) {
          return true;
      }
    }
    return false;
  }


  public static void main (String args[]) {
    if (args.length < 1) {
       System.err.println ("usage: java Filter   ex. java Filter java txt");
       return;
       }
    Filter2 nf = new Filter2 (args);
    // current directory
    File dir = new File (".");
    String[] strs = dir.list(nf);
    for (int i = 0; i < strs.length; i++) {
      System.out.println (strs[i]);
      }
    }
}

 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.