gpt4 book ai didi

Java glob 查找文件

转载 作者:行者123 更新时间:2023-12-01 04:34:20 25 4
gpt4 key购买 nike

我有两个文件 - C:\Test 中的 abc.txt 和 abx.xls。我正在检查如何在 Java 中使用类似 glob 的语法,并发现 below代码:

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

它给了我输出

Matched: 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 项目运行它时,它不起作用。

最佳答案

在我看来,您遇到的程序没有正确检查命令行。它期望 arg[1] 为“-name”,但使用表明它实际上位于 arg[2] 中。使用调试器自己检查一下。

这是一个简单的修复,我将留给您来解决。

关于Java glob 查找文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17546970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com