Here you can find the source of getFileList(String directory, String filterRegexp)
public static String[] getFileList(String directory, String filterRegexp)
//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(); } }