1

C:\Test に abc.txt と abx.xls という 2 つのファイルがあります。Javaでglobのような構文を使用する方法を確認していたところ、以下のコードに出くわしました:

/**
 * 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/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"
 */

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.*;
import static java.nio.file.FileVisitResult.*;
import static java.nio.file.FileVisitOption.*;
import java.util.*;


public class Find {

    public static class Finder
        extends SimpleFileVisitor<Path> {

        private final PathMatcher matcher;
        private int numMatches = 0;

        Finder(String pattern) {
            matcher = FileSystems.getDefault()
                    .getPathMatcher("glob:" + pattern);
        }

        // 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();
    }
}

次に、次のコマンドで実行します。

% java Find "C:\Test" -name "*.*"

それは私に出力を与えます

一致: 0

次に、以下のような簡単なコードを書きました。

PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.*");
File pollDirectoryFile = new File("C:\\Test");
File[] files = pollDirectoryFile.listFiles();
Arrays.sort(files);
for (File file : files) {
        Path path = FileSystems.getDefault().getPath(file.getName());
        if (matcher.matches(path)) {
            System.out.println(path);
        }
}

興味深いことに、実行すると、期待される出力が得られます

abc.txt

abc.xls

誰かが違いを教えてもらえますか?Windows7でJDK7u25を使用しています。

編集:実際にコマンドラインから実行すると、正しい出力が得られました。ただし、Eclipse プロジェクトから実行すると機能しません。

4

1 に答える 1

0

あなたが遭遇したプログラムは、コマンドラインを適切にチェックしていないように見えます。arg[1] には '-name' が必要ですが、使用法から、実際には arg[2] にあることが示唆されます。デバッガーを使用して、これを自分で確認してください。

これは簡単な修正であり、理解するために残します。

于 2013-07-09T11:36:42.457 に答える