集册 Java实例教程 查找与指定glob模式匹配的文件

查找与指定glob模式匹配的文件

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

550
查找与指定的glob模式匹配的文件

import java.io.*;

import java.nio.file.*;

import java.nio.file.attribute.*;

import static java.nio.file.FileVisitResult.*;
/**
来 自 nowjava
**/

import static java.nio.file.FileVisitOption.*;

import java.util.*;


/**

 * Sample code that finds files that

 * match the specified glob pattern.

 * For more information on what

 * constitutes a glob pattern, see

 * http://docs.oracle.com/javase/javatutorials/tutorial/essential/io/fileOps.html#glob

 *

 * The file or directories that match

 * the pattern are printed to

 * standard out.  The number of

 * matches is also printed.

 *

 * When executing this application,

 * you must put the glob pattern

 * in quotes, so the shell will not

 * expand any wild cards:

 *     java Find . -name "*.java"

 */


public class Find {


    /**

     * A {@code FileVisitor} that finds

     * all files that match the

     * specified pattern.

     */

    public static class Finder extends SimpleFileVisitor<Path> {


        private final PathMatcher matcher;

        private int numMatches = 0;


        Finder(String pattern) {

            matcher = FileSystems.getDefault().getPathMatcher(

                    "glob:" + pattern);
                    /**
                    时 代 J a v a 公 众 号 - N o w J a v  a . c o m 提供 
                    **/

        }


        // Compares the glob pattern against

        // the file or directory name.

        void find(Path file) {

            Path name = file.getFileName();

            if (name != null && matcher.matches(name)) {

                numMatches++;

                System.out.println(file);

            }

        }


        // Prints the total number of

        // matches to standard out.

        void done() {

            System.out.println("Matched: " + numMatches);

        }


        // Invoke the pattern matching

        // method on each file.

        @Override

        public FileVisitResult visitFile(Path file,

                BasicFileAttributes attrs) {

            find(file);

            return CONTINUE;

        }


        // Invoke the pattern matching

        // method on each directory.

        @Override

        public FileVisitResult preVisitDirectory(Path dir,

                BasicFileAttributes attrs) {

            find(dir);

            return CONTINUE;

        }


        @Override

        public FileVisitResult visitFileFailed(Path file, IOException exc) {

            System.err.println(exc);

            return CONTINUE;

        }

    }


    static void usage() {

        System.err

                .println("java Find <path>" + " -name \"<glob_pattern>\"");

        System.exit(-1);

    }


    public static void main(String[] args) throws IOException {


        if (args.length < 3 || !args[1].equals("-name"))

            usage();


        Path startingDir = Paths.get(args[0]);

        String pattern = args[2];


        Finder finder = new Finder(pattern);

        Files.walkFileTree(startingDir, finder);

        finder.done();

    }

}


展开阅读全文