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

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

Description

get Files List

License

Open Source License

Declaration

public static List<String> getFiles2List(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<String> getFiles2List(String path, String[] allowedExtension) {
        List<String> r = new ArrayList<String>();
        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.getName());/*from  w w  w .  j  a  v a 2s  .c  o  m*/
                }
            }
        }

        return r;
    }

    public static List<String> getFiles2List(String[] paths, String[] allowedExtension) {
        List<String> r = new ArrayList<String>();
        for (String p : paths) {
            List<String> flist = getFiles2List(p, allowedExtension);
            if (!flist.isEmpty())
                r.addAll(flist);
        }

        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;
    }

    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);
                }
            }
        }

        return r;
    }
}

Related

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