public class RegexFileFilter extends AbstractFileFilter implements java.io.Serializable
See java.util.regex.Pattern for regex matching rules.
e.g.
File dir = new File("."); FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"); File[] files = dir.listFiles(fileFilter); for (String file : files) { System.out.println(file); }
final Path dir = Paths.get(""); final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$")); // // Walk one dir Files.walkFileTree(dir, Collections.emptySet(), 1, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getFileList()); // visitor.getPathCounters().reset(); // // Walk dir tree Files.walkFileTree(dir, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getDirList()); System.out.println(visitor.getFileList());
Modifier and Type | Field and Description |
---|---|
private java.util.function.Function<java.nio.file.Path,java.lang.String> |
pathToString
How convert a path to a string.
|
private java.util.regex.Pattern |
pattern
The regular expression pattern that will be used to match file names.
|
private static long |
serialVersionUID |
EMPTY_STRING_ARRAY
Constructor and Description |
---|
RegexFileFilter(java.util.regex.Pattern pattern)
Constructs a new regular expression filter for a compiled regular expression
|
RegexFileFilter(java.util.regex.Pattern pattern,
java.util.function.Function<java.nio.file.Path,java.lang.String> pathToString)
Constructs a new regular expression filter for a compiled regular expression
|
RegexFileFilter(java.lang.String pattern)
Constructs a new regular expression filter.
|
RegexFileFilter(java.lang.String pattern,
int flags)
Constructs a new regular expression filter with the specified flags.
|
RegexFileFilter(java.lang.String pattern,
IOCase caseSensitivity)
Constructs a new regular expression filter with the specified flags case sensitivity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
accept(java.io.File dir,
java.lang.String name)
Checks to see if the file name matches one of the regular expressions.
|
java.nio.file.FileVisitResult |
accept(java.nio.file.Path path,
java.nio.file.attribute.BasicFileAttributes attributes)
Checks to see if the file name matches one of the regular expressions.
|
private static java.util.regex.Pattern |
compile(java.lang.String pattern,
int flags)
Compiles the given pattern source.
|
private static int |
toFlags(IOCase caseSensitivity)
Converts IOCase to Pattern compilation flags.
|
java.lang.String |
toString()
Returns a debug string.
|
accept, handle, postVisitDirectory, preVisitDirectory, toFileVisitResult, visitFile, visitFileFailed
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
and, negate, or
private static final long serialVersionUID
private final java.util.regex.Pattern pattern
private final java.util.function.Function<java.nio.file.Path,java.lang.String> pathToString
public RegexFileFilter(java.util.regex.Pattern pattern)
pattern
- regular expression to match.java.lang.IllegalArgumentException
- if the pattern is null.public RegexFileFilter(java.util.regex.Pattern pattern, java.util.function.Function<java.nio.file.Path,java.lang.String> pathToString)
pattern
- regular expression to match.pathToString
- How convert a path to a string.java.lang.IllegalArgumentException
- if the pattern is null.public RegexFileFilter(java.lang.String pattern)
pattern
- regular string expression to matchjava.lang.IllegalArgumentException
- if the pattern is nullpublic RegexFileFilter(java.lang.String pattern, int flags)
pattern
- regular string expression to matchflags
- pattern flags - e.g. Pattern.CASE_INSENSITIVE
java.lang.IllegalArgumentException
- if the pattern is nullpublic RegexFileFilter(java.lang.String pattern, IOCase caseSensitivity)
pattern
- regular string expression to matchcaseSensitivity
- how to handle case sensitivity, null means case-sensitivejava.lang.IllegalArgumentException
- if the pattern is nullprivate static java.util.regex.Pattern compile(java.lang.String pattern, int flags)
pattern
- the source pattern.flags
- the compilation flags.private static int toFlags(IOCase caseSensitivity)
caseSensitivity
- case-sensitivity.public boolean accept(java.io.File dir, java.lang.String name)
accept
in interface java.io.FilenameFilter
accept
in interface IOFileFilter
accept
in class AbstractFileFilter
dir
- the file directory (ignored)name
- the file namepublic java.nio.file.FileVisitResult accept(java.nio.file.Path path, java.nio.file.attribute.BasicFileAttributes attributes)
accept
in interface PathFilter
accept
in interface IOFileFilter
path
- the pathattributes
- the path attributespublic java.lang.String toString()
toString
in class AbstractFileFilter