Here you can find the source of getFilesFromExtension(String directory, String[] extensions)
Parameter | Description |
---|---|
directory | The path of the directory. |
extensions | an array of expected extensions. |
public static String[] getFilesFromExtension(String directory, String[] extensions)
//package com.java2s; /* ==================================================================== * The Apache Software License, Version 1.1 * * Copyright (c) 2001 The Apache Software Foundation. All rights * reserved./*w w w. j a v a 2s .c om*/ * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, * if any, must include the following acknowledgment: * "This product includes software developed by the * Apache Software Foundation (http://www.codehaus.org/)." * Alternately, this acknowledgment may appear in the software itself, * if and wherever such third-party acknowledgments normally appear. * * 4. The names "Apache" and "Apache Software Foundation" and * "Apache Turbine" must not be used to endorse or promote products * derived from this software without prior written permission. For * written permission, please contact codehaus@codehaus.org. * * 5. Products derived from this software may not be called "Apache", * "Apache Turbine", nor may "Apache" appear in their name, without * prior written permission of the Apache Software Foundation. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.codehaus.org/>. * */ import java.io.File; import java.util.ArrayList; import java.util.List; public class Main { /** * Given a directory and an array of extensions return an array of compliant files. * <p/> * TODO Should an ignore list be passed in? * TODO Should a recurse flag be passed in? * <p/> * The given extensions should be like "java" and not like ".java" * * @param directory The path of the directory. * @param extensions an array of expected extensions. * @return An array of files for the wanted extensions. */ public static String[] getFilesFromExtension(String directory, String[] extensions) { List<String> files = new ArrayList<String>(); File currentDir = new File(directory); String[] unknownFiles = currentDir.list(); if (unknownFiles == null) { return new String[0]; } for (String unknownFile : unknownFiles) { String currentFileName = directory + System.getProperty("file.separator") + unknownFile; File currentFile = new File(currentFileName); if (currentFile.isDirectory()) { //ignore all CVS directories... if (currentFile.getName().equals("CVS")) { continue; } //ok... transverse into this directory and get all the files... then combine //them with the current list. String[] fetchFiles = getFilesFromExtension(currentFileName, extensions); files = blendFilesToVector(files, fetchFiles); } else { //ok... add the file String add = currentFile.getAbsolutePath(); if (isValidFile(add, extensions)) { files.add(add); } } } //ok... move the Vector into the files list... String[] foundFiles = new String[files.size()]; files.toArray(foundFiles); return foundFiles; } /** * Private helper method for getFilesFromExtension() */ private static List<String> blendFilesToVector(List<String> v, String[] files) { for (String file : files) { v.add(file); } return v; } /** * Checks to see if a file is of a particular type(s). * Note that if the file does not have an extension, an empty string * ("") is matched for. */ private static boolean isValidFile(String file, String[] extensions) { String extension = extension(file); if (extension == null) { extension = ""; } //ok.. now that we have the "extension" go through the current know //excepted extensions and determine if this one is OK. for (String extension1 : extensions) { if (extension1.equals(extension)) { return true; } } return false; } /** * Returns the extension portion of a file specification string. * This everything after the last dot '.' in the filename (NOT including * the dot). * * @param filename the file path * @return the extension of the file */ public static String extension(String filename) { // Ensure the last dot is after the last file separator int lastSep = filename.lastIndexOf(File.separatorChar); int lastDot; if (lastSep < 0) { lastDot = filename.lastIndexOf('.'); } else { lastDot = filename.substring(lastSep + 1).lastIndexOf('.'); if (lastDot >= 0) { lastDot += lastSep + 1; } } if (lastDot >= 0 && lastDot > lastSep) { return filename.substring(lastDot + 1); } return ""; } }