![]() | |
|
The new Java 7 NIO.2 java.nio.file
package provides programmatic support for files locating by pattern matching. Each file system
implementation provides a PathMatcher
interface implementation:
public interface PathMatcher { /** * Tells if given path matches this matcher's pattern. */ boolean matches(Path path); }
You can retrieve a file system's PathMatcher
by using the getPathMatcher(String)
method in the FileSystem
class.
You can get the path matcher from the file system using this code:
FileSystem fs = FileSystems.getDefault(); PathMatcher matcher = fs.getPathMatcher("glob:" + pattern);
The string argument passed to getPathMatcher
specifies the syntax flavor and the pattern to be matched.
This example specifies glob
syntax.
At least two syntaxes are supported (others may be supported by a particular file system implementation):
glob
When the syntax is "glob
" then the String
representation of the path is matched using a limited
pattern language that resembles regular expressions but with a simpler syntax. For example:
*.java
- Matches a path that represents a file name ending in .java
The *
character matches zero or more characters of a name component without crossing directory boundaries.
*.*
- Matches file names containing a dot
*[0-9]*
- Matches file names containing a numeric value.
The [ ]
characters are a bracket expression that match a single character of a name component out of a set of characters.
For example, [abc]
matches "a
", "b
", or "c
". The hyphen (-
) may be used
to specify a range so [a-z]
specifies a range that matches from "a
" to "z
" (inclusive).
These forms can be mixed so [abce-g]
matches "a
", "b
", "c
", "e
", "f
"
or "g
". If the character after the [
is a !
then it is used for negation so [!a-c]
matches any
character except "a
", "b
", or "c
".
*.{java,class}
- Matches file names ending with .java
or .class
The { }
characters are a group of subpatterns, where the group matches if any subpattern in the group matches.
The ",
" character is used to separate the subpatterns. Groups cannot be nested.
foo.?
- Matches file names starting with foo.
and a single character extension
The ?
character matches exactly one character of a name component.
/home/*/*
- Matches /home/mikalai/data
on UNIX platforms
The *
character matches zero or more characters of a name component without crossing directory boundaries.
/home/**
- Matches /home/mikalai
and /home/mikalai/data
on UNIX platforms
The **
characters matches zero or more characters crossing directory boundaries.
C:\\*
- Matches C:\foo
and C:\bar
on the Windows platform (note that the backslash is escaped; as a
string literal in the Java Language the pattern would be "C:\\\\*
")
regex
When the syntax is "regex
" then the pattern component is a regular expression as defined by the java.util.regex.Pattern
class.
For both the glob
and regex
syntaxes, the matching details, such as whether the matching is case sensitive, are implementation-dependent
and therefore not specified.
If you want to use some other form of string-based pattern matching, you can create your own PathMatcher
class.
Once you have created your PathMatcher
instance, you are ready to match files against it. The PathMatcher
interface
has a single method, matches(...)
, that takes a Path
argument and returns a boolean
: it either matches
the pattern, or it does not. The following code snippet looks for files that end in .txt
and .doc
and prints those
files to standard output:
Path startDir = Paths.get("C:\\home"); String pattern = "*.{txt,doc}"; FileSystem fs = FileSystems.getDefault(); final PathMatcher matcher = fs.getPathMatcher("glob:" + pattern); FileVisitor<Path> matcherVisitor = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attribs) { Path name = file.getFileName(); if (matcher.matches(name)) { System.out.print(String.format("Found matched file: '%s'.%n", file)); } return FileVisitResult.CONTINUE; } }; Files.walkFileTree(startDir, matcherVisitor);
the output could be as follows:
Found matched file: 'C:\home\zaikin\foo\company\readme.txt'. Found matched file: 'C:\home\zaikin\foo\resume.doc'. Found matched file: 'C:\home\zaikin\foo\test.txt'.
![]() ![]() ![]() |