Java Directory to File List getAllFiles(String dir, String filePattern)

Here you can find the source of getAllFiles(String dir, String filePattern)

Description

get All Files

License

Open Source License

Declaration

public static String[] getAllFiles(String dir, String filePattern) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FilenameFilter;

import java.util.regex.Pattern;

public class Main {
    public static String[] getAllFiles(String dir, String filePattern) {
        File fd = new File(dir);
        if (!fd.exists() || !fd.isDirectory()) {
            return new String[0];
        }/*  w  w w . j  a  va2 s  .co  m*/
        String rPattern = createRegexFromGlob(filePattern);

        return fd.list(new FilenameFilter() {

            @Override
            public boolean accept(File d, String fileName) {
                return Pattern.matches(rPattern, fileName);
            }
        });
    }

    public static String createRegexFromGlob(String glob) {
        String out = "^";
        for (int i = 0; i < glob.length(); ++i) {
            final char c = glob.charAt(i);
            switch (c) {
            case '*':
                out += ".*";
                break;
            case '?':
                out += '.';
                break;
            case '.':
                out += "\\.";
                break;
            case '\\':
                out += "\\\\";
                break;
            default:
                out += c;
            }
        }
        out += '$';
        return out;
    }
}

Related

  1. getAllFiles(File rootDirectory, FileFilter fileFilter, boolean includeSubdirectories)
  2. getAllFiles(File sourceDirectory, List fileList)
  3. getAllFiles(final String path, final String suffix, boolean recurse)
  4. getAllFiles(String dir)
  5. getAllFiles(String dir, String extension)
  6. getAllFiles(String directory)
  7. getAllFiles(String directory)
  8. getAllFiles(String dirPath)
  9. getAllFiles(String folderPath)