Java Folder Read by Extension getFilesFiteredByExtension(final File parent, final List extensions)

Here you can find the source of getFilesFiteredByExtension(final File parent, final List extensions)

Description

get Files Fitered By Extension

License

Open Source License

Declaration

public static List<File> getFilesFiteredByExtension(final File parent, final List<String> extensions) 

Method Source Code

//package com.java2s;
/**/*from   w w  w  .java2  s.  c o m*/
 *
 * Licensed under the GNU General Public License (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.gnu.org/licenses/gpl.txt
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * 
 * @author Vadim Kisen
 *
 * copyright 2010 by uTest 
 */

import java.io.File;

import java.util.ArrayList;

import java.util.List;

public class Main {
    public static List<File> getFilesFiteredByExtension(final File parent, final List<String> extensions) {
        final List<File> res = new ArrayList<File>();
        if (!parent.isDirectory()) {
            return res;
        }
        for (final File file : parent.listFiles()) {
            if (matchByExtension(file, extensions)) {
                res.add(file);
            }
        }
        return res;
    }

    private static boolean matchByExtension(final File file, final List<String> exts) {
        if (file.isDirectory()) {
            return false;
        }

        final String ext = file.getName().substring(file.getName().lastIndexOf(".") + 1);
        for (final String extension : exts) {
            if (extension.equalsIgnoreCase(ext)) {
                return true;
            }
        }
        return false;

    }
}

Related

  1. getFiles(String dir, String... extension)
  2. getFiles(String directory, String ext)
  3. getFiles(String path, String _exts)
  4. getFiles(String path, String[] allowedExtension)
  5. getFiles2List(String path, String[] allowedExtension)
  6. getFilesForType(final File target, final String extension)
  7. getFilesFromExtension(String directory, String[] extensions)
  8. getFilesWithExtension(File aDirectory, final String aExtension)
  9. getFilesWithExtension(File dir, String extension)