Java File List Load getFileList(String directory, String filterRegexp)

Here you can find the source of getFileList(String directory, String filterRegexp)

Description

get File List

License

Open Source License

Return

a list of the full-path filenames for files in the specified directory, which meet the filter criterion.

Declaration

public static String[] getFileList(String directory, String filterRegexp) 

Method Source Code


//package com.java2s;
/* This file is part of the Joshua Machine Translation System.
 * //from www. j av a  2s.co m
 * Joshua is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 */

import java.io.*;
import java.util.*;

import java.util.regex.*;

public class Main {
    /**
     * @return a list of the full-path filenames for files in the specified
     * directory, which meet the filter criterion.
     */
    public static String[] getFileList(String directory, String filterRegexp) {
        File rootDir = new File(directory);
        String[] allFilenames = rootDir.list();

        java.util.regex.Pattern pattern = java.util.regex.Pattern.compile(filterRegexp);

        List<String> filteredFilenames = new ArrayList<String>();
        for (int i = 0; i < allFilenames.length; i++) {
            Matcher matcher = pattern.matcher(allFilenames[i]);
            if (matcher.find()) {
                filteredFilenames.add(getPath(directory, allFilenames[i]));
            }
        }

        String[] filteredFilenamesArray = new String[filteredFilenames.size()];
        for (int i = 0; i < filteredFilenames.size(); i++) {
            filteredFilenamesArray[i] = (String) filteredFilenames.get(i);
        }
        return filteredFilenamesArray;
    }

    /** 
     * @return the full path filename of the filename in the directory.
     */
    public static String getPath(String directory, String filename) {
        File file = new File(directory, filename);
        return file.getAbsolutePath();
    }
}

Related

  1. getFileList(Map>> map, File dir, T key)
  2. getFileList(String dir)
  3. getFileList(String dir)
  4. getFileList(String dir, final String extension)
  5. getFileList(String dir, final String suffix)
  6. getFileList(String directoryPath)
  7. getFileList(String filePath, FileFilter fileFilter, boolean recursive)
  8. getFileList(String folderPath)
  9. getFileList(String inputFilename)