Java Canonical Path Create getCanonicalFiles2List(List path, String[] allowedExtension)

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

Description

get Canonical Files List

License

Open Source License

Declaration

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

Method Source Code

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

import java.io.File;

import java.io.IOException;

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

public class Main {
    public static List<String> getCanonicalFiles2List(List<String> path, String[] allowedExtension) {
        List<String> r = new ArrayList<String>();
        for (String p : path) {
            List<String> r2 = new ArrayList<String>();
            File rfolder = new File(p);
            if (rfolder.exists() && rfolder.isDirectory()) {
                File[] files = rfolder.listFiles();
                for (File f : files) {
                    if (f.isFile() && !f.getName().startsWith(".")) {
                        if (allowedExtension.length > 0) {
                            if (isAllowedExtension(f.getName(), allowedExtension)) {
                                r2.add(p + "/" + f.getName());
                            }//from w  w w . ja  v  a 2 s  .co  m
                        } else {
                            r2.add(p + "/" + f.getName());
                        }
                    }
                }
                r.addAll(r2);
            }
        }

        return r;
    }

    public static List<String> getCanonicalFiles2List(String[] path, String[] allowedExtension, boolean checkExt) {
        List<String> r = new ArrayList<String>();
        for (String p : path) {
            List<String> r2 = new ArrayList<String>();
            File rfolder = new File(p);
            if (rfolder.exists() && rfolder.isDirectory()) {
                File[] files = rfolder.listFiles();
                for (File f : files) {
                    if (!checkExt || isAllowedExtension(f.getName(), allowedExtension)) {
                        try {
                            r2.add(f.getCanonicalPath());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
                r.addAll(r2);
            }
        }

        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. getCanonicalFile(final File file)
  2. getCanonicalFile(final File file)
  3. getCanonicalFileEL(File file)
  4. getCanonicalFileName(String filename)
  5. getCanonicalFileOrNull(@Nullable final File aFile)
  6. getCanonicalFileURL(File file)
  7. getCanonicalLocationFilePath(String location, String file)
  8. getCanonicalOrAbsolutePath(File file)
  9. getCanonicalPath(File file)