Java Folder Read by Filter getFilesWithFilter(String DirectoryName, FilenameFilter Filter)

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

Description

{ method

License

Apache License

Parameter

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

Return

the array of files or null if none }

Declaration

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

Method Source Code

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

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

public class Main {
    /**/* ww  w.  ja  v  a2s .  c  o  m*/
     * { method
     *
     * @param DirectoryName <Add Comment Here>
     * @param Filter        filter to apply
     * @return the array of files or null if none
     *         }
     * @name getFilesWithFilter
     * @function get all files at the current level passing the filter
     * @UnusedParam> FileName start directory
     */
    public static String[] getFilesWithFilter(String DirectoryName, FilenameFilter Filter) {
        File TestFile = new File(DirectoryName);
        return getFilesWithFilter(TestFile, DirectoryName, Filter);
    }

    public static String[] getFilesWithFilter(File pTestFile, String DirectoryName, FilenameFilter Filter) {
        String test;
        String Files[];
        Vector ret = new Vector();
        if (pTestFile == null) {
            return (null);
        }
        if (!pTestFile.isDirectory()) {
            String rfile[] = new String[1];
            rfile[0] = DirectoryName;
            return (rfile);
        }
        if (Filter == null) {
            Files = pTestFile.list();
        } else {
            Files = pTestFile.list(Filter);
        }
        for (int j = 0; j < Files.length; j++) {
            test = pathConcat(DirectoryName, Files[j]);
            ret.addElement(test);
        }
        String out[] = new String[ret.size()];
        for (int i = 0; i < ret.size(); i++) {
            test = (String) ret.elementAt(i);
            out[i] = test;
        }
        return (out);
    }

    /**
     * { method
     *
     * @param s1 path
     * @param s2 file
     * @return filtered name
     *         }
     * @name pathConcat
     * @function add path and file name or path and subpath adding separator
     * if needed For example if s1 = "C:/foo/bar" and s2 = "Myfile.txt"
     * return "C:/foo/bar/Myfile.txt"
     */
    static public String pathConcat(String s1, String s2) {
        if (s1 == null || s1.length() == 0) {
            return (s2);
        }
        if (s2 == null || s2.length() == 0) {
            return (s1);
        }
        char lastchar = s1.charAt(s1.length() - 1);
        if (lastchar == File.separatorChar || lastchar == '/') {
            return (s1 + s2);
        } else {
            return (s1 + "/" + s2);
        }
    }
}

Related

  1. getFiles(String sourceDirPath, FilenameFilter filter, Comparator comparator)
  2. getFiles(String sourceName, FilenameFilter filter)
  3. getFilesOnlyFilter()
  4. getFilesRealWorking(File aStartingDir, FilenameFilter filter)
  5. getFilesUnder(String path, FileFilter fileFilter)