Here you can find the source of getFilesWithFilter(String DirectoryName, FilenameFilter Filter)
Parameter | Description |
---|---|
DirectoryName | <Add Comment Here> |
Filter | filter to apply |
public static String[] getFilesWithFilter(String DirectoryName, FilenameFilter Filter)
//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); } } }