Package com.pnfsoftware.jeb.util.io
Class DirectoryEnumerator
java.lang.Object
com.pnfsoftware.jeb.util.io.DirectoryEnumerator
List (full in-memory enumeration) or iterate over a directory contents, with support for
recursion and regex file filters. The returned File Objects are real
Files passing
the File.isFile() test.-
Constructor Summary
ConstructorsConstructorDescriptionDirectoryEnumerator(File base) Create a new directory enumerator.DirectoryEnumerator(File base, String filterString, boolean recurse) Create a new directory enumerator. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddBlackListedDirectory(String dirname) Record a simple directory name (not path) that should not be recursively explored.Get the directory names skipped during recursive enumeration.Get the active regex filter.booleanDetermine whether recursive enumeration is enabled.iterator()list()Browse the folder(s) and retrieve the matching files.voidSet a regex pattern to select files.voidsetFilterString(String filterString, boolean matchCaseInsensitive, boolean matchUnicode) Set a wildcard filename filter.voidsetRecurse(boolean recurse) Enable or disable recursive enumeration.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DirectoryEnumerator
Create a new directory enumerator.- Parameters:
base- base folderfilterString- optional wildcard filter string for filenames; to use a full regex pattern, provide null for this parameter and usesetFilter(Pattern)after constructionrecurse- true to recursively enumerate sub-directories.- Throws:
IllegalArgumentException- on initialization error
-
DirectoryEnumerator
Create a new directory enumerator. The enumerator is filter-less (it matches on all files) and does not recurse over sub-directories.- Parameters:
base- base folder
-
-
Method Details
-
setFilterString
public void setFilterString(String filterString, boolean matchCaseInsensitive, boolean matchUnicode) Set a wildcard filename filter.- Parameters:
filterString- non-null filter string; to disable filters, usesetFilter(Pattern)matchCaseInsensitive- true to match case-insensitivelymatchUnicode- true to enable Unicode character classes
-
setFilter
Set a regex pattern to select files.Example:
".*\\.(exe|dll)$"will match all Windows executables and dynamic libraries.- Parameters:
pattern- optional regex pattern, null to disable
-
getFilter
Get the active regex filter.- Returns:
- regex pattern, or null if no filter is active
-
setRecurse
public void setRecurse(boolean recurse) Enable or disable recursive enumeration.- Parameters:
recurse- true to recurse into subdirectories
-
isRecurse
public boolean isRecurse()Determine whether recursive enumeration is enabled.- Returns:
- true if subdirectories are recursively explored
-
addBlackListedDirectory
Record a simple directory name (not path) that should not be recursively explored. Ignored if the enumerator is not recursive. The base directory is always explored, even if its simple name is blacklisted.Example: 'xyz' is blacklisted, the search is recursive and starts in '/xyz':
- "/xyz/..." is explored, no matter what
- if it exists, "/xyz/xyz/..." and its sub-dirs WONT be explored
- if it exists, "/xyz/0/1/xyz/..." and its sub-dirs WONT be explored
- if it exists, file named '/xyz/xyz' or /xyz/foo/xyz" WILL be returned.- Parameters:
dirname- simple directory name to skip during recursion
-
getBlacklistedDirnames
Get the directory names skipped during recursive enumeration.- Returns:
- unmodifiable collection of blacklisted simple directory names
-
list
Browse the folder(s) and retrieve the matching files.- Returns:
- a list of files
-
iterator
-