Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Pattern Based file listing of a directory in java

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
maruthi123
Asked:
maruthi123
1 Solution
 
Venci75Commented:
if you use java 1.4, that accept method could be like this:
return (name.matches("reg exp"));
0
 
CEHJCommented:
If you're going to use regular expressions, be careful, as they are not the same as wildcards.
0
 
CleanupPingCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now