Glob mode in Java NIO

Source: Internet
Author: User

Glob mode in Java NIO
Glob mode in Java NIO
1. What is Glob?
In programming, Glob is a pattern that uses wildcards to specify file names. For example, *. java is a simple Glob, which specifies all files with the extension "java. In Glob mode, two wildcard characters "*" and "?" are widely used. 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:


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.

 
  1. package com.javapapers.java.nio;

  2. import java.io.IOException;
  3. import java.nio.file.FileSystems;
  4. import java.nio.file.FileVisitResult;
  5. import java.nio.file.Files;
  6. import java.nio.file.Path;
  7. import java.nio.file.PathMatcher;
  8. import java.nio.file.Paths;
  9. import java.nio.file.SimpleFileVisitor;
  10. import java.nio.file.attribute.BasicFileAttributes;

  11. public class FileGlobNIO {

  12. public static void main(String args[]) throws IOException {
  13. String glob = "glob:**/*.zip";
  14. String path = "D:/";
  15. match(glob, path);
  16. }

  17. public static void match(String glob, String location) throws IOException {

  18. final PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher(
  19. glob);

  20. Files.walkFileTree(Paths.get(location), new SimpleFileVisitor () {

  21. @Override
  22. public FileVisitResult visitFile(Path path,
  23. BasicFileAttributes attrs) throws IOException {
  24. if (pathMatcher.matches(path)) {
  25. System.out.println(path);
  26. }
  27. return FileVisitResult.CONTINUE;
  28. }

  29. @Override
  30. public FileVisitResult visitFileFailed(Path file, IOException exc)
  31. throws IOException {
  32. return FileVisitResult.CONTINUE;
  33. }
  34. });
  35. }

  36. }

The program output is as follows:
  
  1. D:\AndroidLocation.zip
  2. D:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\.tasks.xml.zip
  3. D:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\repositories.xml.zip
  4. D:\Eclipse\7dec2014\eclipse-jee-kepler-R-win32-x86_64\workspace\.metadata\.mylyn\tasks.xml.zip
  5. D:\mysql-workbench-community-6.2.5-winx64-noinstall.zip
  6. D:\workspace\Android\AndroidChatBubbles-master.zip
  7. D:\workspace\Android\Google Chat\XMPPChatDemo.zip
  8. D:\workspace\Android\Update-Android-UI-from-a-Service-master.zip
  9. D:\workspace\Android Chat\AndroidDialog.zip
  10. D:\workspace\Android Wear\AndroidWearPreview.zip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.