Home  >  Java 7

Example of PathMatcher in Java NIO

By Arvind Rai, December 11, 2013
java.nio.file.PathMatcher is an interface introduced in JDK 7. It has a method matches() that matches the pattern with given path. PathMatcher is instantiated by getPathMatcher() which is the method of java.nio.file.FileSystem. Find the syntax below.
PathMatcher getPathMatcher(String synatxNpattern)
We need to pass string in given format.
syntax:pattern
Here syntax can be "glob" and "regex".
While using "glob" as a syntax, we can use below patterns.

*.java when given path is java , we will get true by PathMatcher.matches(path).
*.* if file contains a dot, pattern will be matched.
*.{java,txt} If file is either java or txt, path will be matched.
abc.? matches a file which start with abc and it has extension with only single character.

Example for *.java pattern.
package com.concretepage.io.file;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
public class PathMatcherExample {
	public static void main(String[] args) {
		FileSystem fileSystem = FileSystems.getDefault();
		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:D:/**/*.java");
		Path path = Paths.get("D:/cp/PathMatcherExample.java");
		System.out.println(pathMatcher.matches(path));
	}
}
 
Example for *.{java,txt} pattern.
package com.concretepage.io.file;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
public class PathMatcherExample {
	public static void main(String[] args) {
		FileSystem fileSystem = FileSystems.getDefault();
		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:**.{java,class,txt}");
		Path path = Paths.get("D:/cp/file.txt");
		System.out.println(pathMatcher.matches(path));
	}
}
 
Example for abc.? pattern.
package com.concretepage.io.file;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;
public class PathMatcherExample {
	public static void main(String[] args) {
		FileSystem fileSystem = FileSystems.getDefault();
		PathMatcher pathMatcher = fileSystem.getPathMatcher("glob:**/testFile.?");
		Path path = Paths.get("D:/cp/testFile.t");
		System.out.println(pathMatcher.matches(path));
	}
}
 
To understand "glob" pattern, find the rules with which it interprets the pattern.

* It matches zero , one or more than one characters. While matching, it will not cross directories boundaries.
** It does the same as * but it crosses the directory boundaries.
? It matches only one character for the given name.
\ It helps to avoid characters to be interpreted as special characters.
[] In a set of characters, only single character is matched. If (-) hyphen is used then, it matches a range of characters. Example: [efg] matches "e","f" or "g" . [a-d] matches a range from a to d.
{} It helps to matches the group of sub patterns.

In case of "regex", pattern is regular expression defined by java.util.regex.Pattern. Read java.util.regex.Pattern to get "regex" more.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us