Java Directory to File List getAllFilesWithFilter(String DirectoryName, FilenameFilter Filter)

Here you can find the source of getAllFilesWithFilter(String DirectoryName, FilenameFilter Filter)

Description

{ method

License

Apache License

Parameter

Parameter Description
DirectoryName <Add Comment Here>
Filter test to apply

Return

the array of files or null if none }

Declaration

public static String[] getAllFilesWithFilter(String DirectoryName, FilenameFilter Filter) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.util.*;

public class Main {
    /**// w  w  w. jav a  2 s.com
     * { method
     *
     * @param DirectoryName <Add Comment Here>
     * @param Filter        test to apply
     * @return the array of files or null if none
     *         }
     * @name getAllFilesWithFilter
     * @function recursively get all files passing the filter
     * @UnusedParam> FileName start directory
     */
    public static String[] getAllFilesWithFilter(String DirectoryName, FilenameFilter Filter) {
        File TestFile = new File(DirectoryName);
        return (getAllFilesWithFilter(TestFile, Filter));
    }

    /**
     * { method
     *
     * @param DirectoryName <Add Comment Here>
     * @param Filter        test to apply
     * @return the array of files or null if none
     *         }
     * @name getAllFilesWithFilter
     * @function recursively get all files passing the filter
     * @UnusedParam> FileName start directory
     */
    public static String[] getAllFilesWithFilter(File TestFile, FilenameFilter Filter) {
        String test;
        File Subfile;
        String SubFiles[];
        String Files[];
        Vector ret = new Vector();
        if (TestFile == null) {
            return (null);
        }
        if (!TestFile.isDirectory()) {
            String rfile[] = new String[1];
            rfile[0] = TestFile.getPath().replace('\\', '/');
            return (rfile);
        }
        if (Filter == null) {
            Files = TestFile.list();
        } else {
            Files = TestFile.list(Filter);
        }
        for (int j = 0; j < Files.length; j++) {
            Subfile = new File(TestFile, Files[j]);
            if (Subfile.isDirectory()) {
                SubFiles = getAllFilesWithFilter(Subfile, Filter);
                for (int k = 0; k < SubFiles.length; k++) {
                    ret.addElement(SubFiles[k]);
                }
            } else {
                String path = null;
                /*    try {
                path = Subfile.getCanonicalPath();
                String base =  new File(".").getCanonicalPath() ;
                path = path.substring(base.length());
                }
                catch(IOException ex) {
                */
                path = Subfile.getPath();
                //  }

                ret.addElement(path.replace('\\', '/'));
            }
        }
        String out[] = new String[ret.size()];
        for (int i = 0; i < ret.size(); i++) {
            test = (String) ret.elementAt(i);
            out[i] = test;
        }
        return (out);
    }
}

Related

  1. getAllFilesPresentInFolder(File srcPath)
  2. getAllFilesRecursively(File dir, List filelist)
  3. getAllFilesRecursively(File directory, Collection files)
  4. getAllFilesWithExtension(String directory, final String extension)
  5. getAllFilesWithExtension(String folderPath, String extension)