Java File List Load getFileList(String path, String regex)

Here you can find the source of getFileList(String path, String regex)

Description

Gets the file list.

License

Open Source License

Parameter

Parameter Description
path the path
regexExpression the regexExpression

Return

the file list

Declaration

public static String[] getFileList(String path, String regex) 

Method Source Code

//package com.java2s;
/*/*from   www  .ja  v  a 2 s. co  m*/
 * Copyright (C) 2010, 2011, 2012 by Arne Kesting, Martin Treiber, Ralph Germ, Martin Budden
 * <movsim.org@gmail.com>
 * -----------------------------------------------------------------------------------------
 * 
 * This file is part of
 * 
 * MovSim - the multi-model open-source vehicular-traffic simulator.
 * 
 * MovSim is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * MovSim is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with MovSim. If not, see <http://www.gnu.org/licenses/>
 * or <http://www.movsim.org>.
 * 
 * -----------------------------------------------------------------------------------------
 */

import java.io.File;

import java.io.FilenameFilter;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
    /**
     * Gets the file list.
     * 
     * @param path
     *            the path
     * @param regexExpression
     *            the regexExpression
     * @return the file list
     */
    public static String[] getFileList(String path, String regex) {
        final File dir = new File(path);

        class PatternFilter implements FilenameFilter {
            final String regexExpression;

            public PatternFilter(String regex) {
                regexExpression = regex;
            }

            @Override
            public boolean accept(File f, String name) {
                final Pattern patternRegex = Pattern.compile(regexExpression);
                final Matcher matcher = patternRegex.matcher(name);
                final boolean matches = matcher.matches();
                return (matches);
            }
        }

        final String[] fileNames = dir.list(new PatternFilter(regex));

        if (fileNames != null) {
            for (int i = 0; i < fileNames.length; i++) {
                fileNames[i] = path + fileNames[i];
            }
        }
        return (fileNames);
    }
}

Related

  1. getFileList(String path)
  2. getFileList(String path)
  3. getFileList(String path)
  4. getFileList(String path)
  5. getFileList(String path, String filterName)
  6. getFileList(String path, String searchString)
  7. getFileList(String s)
  8. getFileList(String zipFilePath)
  9. getFileListByDFS(File file)