?
Solved

Pattern Based file listing of a directory in java

Posted on 2003-02-20
3
Medium Priority
?
172 Views
Last Modified: 2010-03-31
Hi

I want to list files in a directory based on a pattern.
The FilenameFilter Interface is used for that.

eg:
import java.io.FilenameFilter;
import java.io.File;

class JavaSrcFilter implements FilenameFilter {
    public boolean accept(File dir, String name) {
        return (name.endsWith("*.java"));
    }
}

class Main {
    public static void main (String[] args) {
        String dir = ".";
        if (args.length == 1)
            dir = args[0];

        File f1 = new File(dir);
        int i;
        String[] ls;
        FilenameFilter filter = new JavaSrcFilter();
        System.out.println("Java Source Files: " );
        for (ls = f1.list(filter), i = 0;
             ls != null && i < ls.length;
             System.out.println("\t" + ls[i++]));
    }
}

But the above just checks for some particular condition like files ending with .java
 
Any help is greatful in making it generalised such that it works for patterns like
a. *.*
b. *a?b.*
c.  [abc]?*

i.e accepting all types of possible patterns

thanking you in advance
maruthi.ch


0
Comment
Question by:maruthi123
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 9

Expert Comment

by:Venci75
ID: 7986468
if you use java 1.4, that accept method could be like this:
return (name.matches("reg exp"));
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 40 total points
ID: 7987382
If you're going to use regular expressions, be careful, as they are not the same as wildcards.
0
 

Expert Comment

by:CleanupPing
ID: 9059216
maruthi123:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month11 days, 8 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question