Java Folder Read by Extension getFiles(String path, String[] allowedExtension)

Here you can find the source of getFiles(String path, String[] allowedExtension)

Description

get Files

License

Open Source License

Declaration

public static List<File> getFiles(String path, String[] allowedExtension) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static List<File> getFiles(String path, String[] allowedExtension) {
        List<File> r = new ArrayList<File>();
        File rfolder = new File(path);
        if (rfolder.exists() && rfolder.isDirectory()) {
            File[] files = rfolder.listFiles();
            for (File f : files) {
                if (isAllowedExtension(f.getName(), allowedExtension)) {
                    r.add(f);/*w  w w  .  ja v a2 s  .c o m*/
                }
            }
        }

        return r;
    }

    public static File[] listFiles(String path, String[] allowedExtension) {
        List<File> fs = getFiles(path, allowedExtension);

        return fs.toArray(new File[fs.size()]);
    }

    private static boolean isAllowedExtension(String fExt, String[] allowedExtension) {
        for (String ext : allowedExtension) {
            if (fExt.endsWith(ext))
                return true;
        }
        return false;
    }
}

Related

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