How to find files that match a wildcard string in Java?

前端 未结 16 920
慢半拍i
慢半拍i 2020-11-22 10:52

This should be really simple. If I have a String like this:

../Test?/sample*.txt

then what is a generally-accepted way to get a list of fil

相关标签:
16条回答
  • 2020-11-22 10:55

    The Apache filter is built for iterating files in a known directory. To allow wildcards in the directory also, you would have to split the path on '\' or '/' and do a filter on each part separately.

    0 讨论(0)
  • 2020-11-22 10:56

    The wildcard library efficiently does both glob and regex filename matching:

    http://code.google.com/p/wildcard/

    The implementation is succinct -- JAR is only 12.9 kilobytes.

    0 讨论(0)
  • 2020-11-22 10:57

    You should be able to use the WildcardFileFilter. Just use System.getProperty("user.dir") to get the working directory. Try this:

    public static void main(String[] args) {
    File[] files = (new File(System.getProperty("user.dir"))).listFiles(new WildcardFileFilter(args));
    //...
    }
    

    You should not need to replace * with [.*], assuming wildcard filter uses java.regex.Pattern. I have not tested this, but I do use patterns and file filters constantly.

    0 讨论(0)
  • 2020-11-22 11:02

    As posted in another answer, the wildcard library works for both glob and regex filename matching: http://code.google.com/p/wildcard/

    I used the following code to match glob patterns including absolute and relative on *nix style file systems:

    String filePattern = String baseDir = "./";
    // If absolute path. TODO handle windows absolute path?
    if (filePattern.charAt(0) == File.separatorChar) {
        baseDir = File.separator;
        filePattern = filePattern.substring(1);
    }
    Paths paths = new Paths(baseDir, filePattern);
    List files = paths.getFiles();
    

    I spent some time trying to get the FileUtils.listFiles methods in the Apache commons io library (see Vladimir's answer) to do this but had no success (I realise now/think it can only handle pattern matching one directory or file at a time).

    Additionally, using regex filters (see Fabian's answer) for processing arbitrary user supplied absolute type glob patterns without searching the entire file system would require some preprocessing of the supplied glob to determine the largest non-regex/glob prefix.

    Of course, Java 7 may handle the requested functionality nicely, but unfortunately I'm stuck with Java 6 for now. The library is relatively minuscule at 13.5kb in size.

    Note to the reviewers: I attempted to add the above to the existing answer mentioning this library but the edit was rejected. I don't have enough rep to add this as a comment either. Isn't there a better way...

    0 讨论(0)
  • 2020-11-22 11:02

    Glob of Java7: Finding Files. (Sample)

    0 讨论(0)
  • 2020-11-22 11:03

    Consider DirectoryScanner from Apache Ant:

    DirectoryScanner scanner = new DirectoryScanner();
    scanner.setIncludes(new String[]{"**/*.java"});
    scanner.setBasedir("C:/Temp");
    scanner.setCaseSensitive(false);
    scanner.scan();
    String[] files = scanner.getIncludedFiles();
    

    You'll need to reference ant.jar (~ 1.3 MB for ant 1.7.1).

    0 讨论(0)
提交回复
热议问题