Package org.apache.commons.io.filefilter
Class RegexFileFilter
- java.lang.Object
-
- org.apache.commons.io.filefilter.AbstractFileFilter
-
- org.apache.commons.io.filefilter.RegexFileFilter
-
- All Implemented Interfaces:
java.io.FileFilter,java.io.FilenameFilter,java.io.Serializable,java.nio.file.FileVisitor<java.nio.file.Path>,java.nio.file.PathMatcher,PathFilter,PathVisitor,IOFileFilter
public class RegexFileFilter extends AbstractFileFilter implements java.io.Serializable
Filters files using supplied regular expression(s).See java.util.regex.Pattern for regex matching rules.
Using Classic IO
e.g.
File dir = FileUtils.current(); FileFilter fileFilter = new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$"); File[] files = dir.listFiles(fileFilter); for (String file : files) { System.out.println(file); }Using NIO
final Path dir = PathUtils.current(); final AccumulatorPathVisitor visitor = AccumulatorPathVisitor.withLongCounters(new RegexFileFilter("^.*[tT]est(-\\d+)?\\.java$")); // // Walk one directory Files.walkFileTree(dir, Collections.emptySet(), 1, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getFileList()); // visitor.getPathCounters().reset(); // // Walk directory tree Files.walkFileTree(dir, visitor); System.out.println(visitor.getPathCounters()); System.out.println(visitor.getDirList()); System.out.println(visitor.getFileList());Deprecating Serialization
Serialization is deprecated and will be removed in 3.0.
- Since:
- 1.4
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.function.Function<java.nio.file.Path,java.lang.String>pathToStringHow convert a path to a string.private java.util.regex.PatternpatternThe regular expression pattern that will be used to match file names.private static longserialVersionUID-
Fields inherited from interface org.apache.commons.io.filefilter.IOFileFilter
EMPTY_STRING_ARRAY
-
-
Constructor Summary
Constructors Constructor Description 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 ioCase)Constructs a new regular expression filter with the specified flags case sensitivity.RegexFileFilter(java.util.regex.Pattern pattern)Constructs a new regular expression filter for a compiled regular expressionRegexFileFilter(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
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanaccept(java.io.File dir, java.lang.String name)Checks to see if the file name matches one of the regular expressions.java.nio.file.FileVisitResultaccept(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.Patterncompile(java.lang.String pattern, int flags)Compiles the given pattern source.private static inttoFlags(IOCase ioCase)Converts IOCase to Pattern compilation flags.java.lang.StringtoString()Returns a debug string.-
Methods inherited from class org.apache.commons.io.filefilter.AbstractFileFilter
accept, append, append, get, handle, postVisitDirectory, preVisitDirectory, toDefaultFileVisitResult, toFileVisitResult, visitFile, visitFileFailed
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.apache.commons.io.filefilter.IOFileFilter
and, matches, negate, or
-
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
pattern
private final java.util.regex.Pattern pattern
The regular expression pattern that will be used to match file names.
-
pathToString
private final transient java.util.function.Function<java.nio.file.Path,java.lang.String> pathToString
How convert a path to a string.
-
-
Constructor Detail
-
RegexFileFilter
public RegexFileFilter(java.util.regex.Pattern pattern)
Constructs a new regular expression filter for a compiled regular expression- Parameters:
pattern- regular expression to match.- Throws:
java.lang.NullPointerException- if the pattern is null.
-
RegexFileFilter
public 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- Parameters:
pattern- regular expression to match.pathToString- How convert a path to a string.- Throws:
java.lang.NullPointerException- if the pattern is null.- Since:
- 2.10.0
-
RegexFileFilter
public RegexFileFilter(java.lang.String pattern)
Constructs a new regular expression filter.- Parameters:
pattern- regular string expression to match- Throws:
java.lang.NullPointerException- if the pattern is null
-
RegexFileFilter
public RegexFileFilter(java.lang.String pattern, int flags)Constructs a new regular expression filter with the specified flags.- Parameters:
pattern- regular string expression to matchflags- pattern flags - e.g.Pattern.CASE_INSENSITIVE- Throws:
java.lang.IllegalArgumentException- if the pattern is null
-
RegexFileFilter
public RegexFileFilter(java.lang.String pattern, IOCase ioCase)Constructs a new regular expression filter with the specified flags case sensitivity.- Parameters:
pattern- regular string expression to matchioCase- how to handle case sensitivity, null means case-sensitive- Throws:
java.lang.IllegalArgumentException- if the pattern is null
-
-
Method Detail
-
compile
private static java.util.regex.Pattern compile(java.lang.String pattern, int flags)Compiles the given pattern source.- Parameters:
pattern- the source pattern.flags- the compilation flags.- Returns:
- a new Pattern.
-
toFlags
private static int toFlags(IOCase ioCase)
Converts IOCase to Pattern compilation flags.- Parameters:
ioCase- case-sensitivity.- Returns:
- Pattern compilation flags.
-
accept
public boolean accept(java.io.File dir, java.lang.String name)Checks to see if the file name matches one of the regular expressions.- Specified by:
acceptin interfacejava.io.FilenameFilter- Specified by:
acceptin interfaceIOFileFilter- Overrides:
acceptin classAbstractFileFilter- Parameters:
dir- the file directory (ignored)name- the file name- Returns:
- true if the file name matches one of the regular expressions
-
accept
public 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.- Specified by:
acceptin interfaceIOFileFilter- Specified by:
acceptin interfacePathFilter- Parameters:
path- the pathattributes- the path's basic attributes (may be null).- Returns:
- true if the file name matches one of the regular expressions
-
toString
public java.lang.String toString()
Returns a debug string.- Overrides:
toStringin classAbstractFileFilter- Returns:
- a String representation
- Since:
- 2.10.0
-
-