集册 Java实例教程 使用PathMatcher接口筛选目录

使用PathMatcher接口筛选目录

欢马劈雪     最近更新时间:2020-01-02 10:19:05

512
java.nio.file.PathMatcher接口提供了一种使用glob匹配文件名的方法。

import java.io.IOException;
/**来自 
 N o w  J a v a  .   c o m**/

import java.nio.file.DirectoryIteratorException;

import java.nio.file.DirectoryStream;

import java.nio.file.FileSystems;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.PathMatcher;

import java.nio.file.Paths;


public class Main {

  public static void main(String[] args) {

    // Using the PathMatcher interface to filter a directory

    Path directory = Paths.get("C:/Program Files/Java/jdk1.7.0/bin");

    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:java?.exe");/** 来 自 时 代 J a v a - nowjava.com**/

    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory, "java*.exe")) {

      for (Path file : directoryStream) {

        if (pa
展开阅读全文