Java NIO Glob mode details, javanioglob details
Glob mode in Java NIO
Author: chszs, reprinted with note. Blog homepage: http://blog.csdn.net/chszs
1. What is Glob?
In programming, 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"? ". The asterisk represents "any character or character to form a string", while the question mark represents "any single character ".
Glob mode originated from Unix operating systems. Unix provides a "global command", which can be abbreviated as glob. The Glob mode is similar to a regular expression, but it has limited functions.
Ii. Glob mode in Java NIO
The Glob mode is introduced in the NIO library of Java SE7, which is used in the FileSystem class and used in the PathMatcher getPathMatcher (String syntaxAndPattern) method. Glob can be passed as a parameter to PathMatcher. Similarly, you can use Glob to traverse the entire directory in the Files class.
The Glob mode used in Java NIO is as follows:
Glob Mode |
Description |
*. Txt |
Matching all files with the extension name .txt |
*. {Html, htm} |
Match All files with an extension name of .html or .htm. {} Is used for group mode. It is separated by commas (,). |
?. Txt |
Matching the file name and the extension name is. txt. |
. |
Match All files with extensions |
C: \ Users \* |
Match All files in the Users directory of drive C. The Backslash "\" is used to escape followed characters. |
/Home /** |
Match All files in the/home directory and subdirectory on UNIX platform. ** Used to match the current directory and all its subdirectories |
Invalid xyzw..txt |
A single token is used as the file name, and a single token only contains three types, namely xforwarded_xforwarded_xforwarded_xforwarded_zforwarded', And the extension name is .txt. Square brackets [] are used to specify a set. |
Certificate a-c0000.txt |
A file name is used as the file name. A single file contains only files named "a133", "bits", and "cnames", and files named ".txt. The minus sign "-" is used to specify a range and can only be used in square brackets []. |
[! A0000.txt |
The name of a file must be unique, and the name of a file cannot be included in the file. txt. Exclamation point "!" Used for Negation |
Iii. Glob example in Java NIO
The following 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 IOException { String glob = "glob:**/*.zip"; String path = "D:/"; match(glob, path); } public static void match(String glob, String location) throws IOException { final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher( glob); Files.walkFileTree(Paths.get(location), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path path, BasicFileAttributes attrs) throws IOException { if (pathMatcher.matches(path)) { System.out.println(path); } return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, IOException exc) throws 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