A detailed description of glob patterns in Java NiO
CHSZS, reprint need to indicate. Blog home: Http://blog.csdn.net/chszs
First, what is Glob?
In programming design, Glob is a pattern that uses wildcards to specify file names. For example:. Java is a simple glob that specifies all files with the extension "Java". Two wildcard characters "" and "?" are widely used in the Glob mode. Where the asterisk denotes "any character or character consisting of a string" and the question mark denotes "any single character".
Glob mode originates from the UNIX operating system, UNIX provides a "global command", which can be abbreviated as GLOB. The glob pattern is similar to regular expressions, but it has limited functionality.
Ii. glob patterns in Java NiO
The glob pattern is introduced in the NIO Library of Java SE7, which is used in the FileSystem class and is used in the Pathmatcher getpathmatcher (String syntaxandpattern) method. Glob can be passed as a parameter to Pathmatcher. Similarly, glob can be used in the files class to traverse the entire directory.
The following is the glob pattern used in Java NIO:
glob Mode |
Description |
*.txt |
Match all files with a. txt extension. |
*. {html,htm} |
Matches all files with an. html or. htm extension. {} is used for group mode, which uses commas to separate |
?. Txt |
Matches any single character file with a filename and a. txt extension. |
. |
Match all files with extension |
c:\users\* |
Match all files in the C disk users directory. Backslash "\" is used to escape the immediately following character |
/home/** |
The UNIX platform matches all files in the/home directory and subdirectories. * * Used to match the current directory and all its subdirectories |
[Xyz].txt |
Matches all individual characters as file names, and a single character contains only one of "X" or "Y" or "Z" three, and a file with the extension. txt. square brackets [] are used to specify a collection |
[A-c].txt |
Matches all individual characters as file names, and a single character contains only one of "a" or "B" or "C" three, and a file with a. txt extension. The minus "-" is used to specify a range and can only be used in square brackets [] |
[!a].txt |
Matches all individual characters as file names, and a single character cannot contain the letter "a" and a file with a. txt extension. Exclamation mark "!" Used to negate |
Iii. examples of glob in Java NiO
Below is a Java program that uses the Glob mode to search for a specified directory and its subdirectories.
Package Com.javapapers.java.nio;import Java.io.ioexception;import Java.nio.file.filesystems;import Java.nio.file.filevisitresult;import Java.nio.file.files;import Java.nio.file.path;import Java.nio.file.pathmatcher;import Java.nio.file.paths;import Java.nio.file.simplefilevisitor;import Java.nio.file.attribute.basicfileattributes;public class Fileglobnio {public static void main (String args[]) throws IO Exception {String glob = "Glob:**/*.zip"; String Path = "d:/"; Match (glob, path); public static void Match (string glob, string location) throws IOException {final Pathmatcher pathmatcher = Fi Lesystems.getdefault (). Getpathmatcher (Glob); Files.walkfiletree (Paths.get), new simplefilevisitor<path> () {@Override public Filev Isitresult visitfile (path path, Basicfileattributes attrs) throws IOException {if (path Matcher.matches (Path)) {SysteM.OUT.PRINTLN (path); } return filevisitresult.continue; } @Override Public Filevisitresult visitfilefailed (Path file, IOException exc) th Rows IOException {return filevisitresult.continue; } }); }}
The program output is as follows:
D:\AndroidLocation.zipD:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\.tasks.xml.zipD:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\repositories.xml.zipD:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\tasks.xml.zipD:\mysql-workbench-community-6.2.5-winx64-noinstall.zipD:\workspace\Android\AndroidChatBubbles-master.zipD:\workspace\Android\Google Chat\XMPPChatDemo.zipD:\workspace\Android\Update-Android-UI-from-a-Service-master.zipD:\workspace\Android Chat\AndroidDialog.zipD:\workspace\Android Wear\AndroidWearPreview.zip
A detailed description of glob patterns in Java NiO