如何在JDK7中使用目录通配符 [英] How do I use directory globbing in JDK7
问题描述
我一直在尝试在JDK7中使用新的globbing功能,从文档和示例开始
I have been trying to use the new globbing feature in JDK7, starting from the documentation and examples
我可以使用glob:*。dat
这样的小数据来处理
I can get globs such as "glob:*.dat"
to work with the
Files.walkFileTree(startingDir, finder);
示例但我无法获得**
语法工作。我希望能够创建类似的东西:
example but I have been unable to get the "**"
syntax working. I would like to be able to create something like:
matcher = FileSystems.getDefault().getPathMatcher("glob:" + "foo/**/bar/*.dat");
并会感谢一个简单的例子。我正在使用Windows 7.
and would be grateful for a simple example. I am using Windows 7.
更新:
@Oleg和@JBNizet明确指出/语法与操作系统无关。请注意 Javadocs 建议依赖操作系统的语法也是可能的(?需要)
UPDATE: @Oleg and @JBNizet make it clear that the "/" syntax is OS-independent. Note that the Javadocs suggest that OS-dependent syntax is also possible (?required)
仍然存在问题:
已经采取@Nizet并编辑如下:
STILL PROBLEMS: Have taken @Nizet and edited as follows:
@Test
public void testStackoverflowGlobber() throws IOException {
final PathMatcher matcher =
FileSystems.getDefault().getPathMatcher("glob:*.cml");
Files.walkFileTree(Paths.get("d:/petermr-workspace/jumbo-converters/jumbo-converters-cli/src/test/resources"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
System.out.println("try>> "+file);
if (matcher.matches(file)) {
System.out.println("MATCHES>>"+file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
这会产生如下输出:
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cdx\r19.cdx
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cdx\r19.cdxml
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cdx\r19.cml
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cdx\r19.ref.cdxml
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cdx\r19.ref.cml
try>> d:\petermr-workspace\jumbo-converters\jumbo-converters-cli\src\test\resources\examples\cif\aa2004.cml
但没有匹配的证据
推荐答案
这是一个工作示例,显示 d:/
的任何后代目录中的所有zip文件:
Here's a working example which displays all the zip files in any descendant directory of d:/
:
public static void main(String[] args) throws IOException {
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:d:/**/*.zip");
Files.walkFileTree(Paths.get("d:/"), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (matcher.matches(file)) {
System.out.println(file);
}
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
return FileVisitResult.CONTINUE;
}
});
}
如您所见,使用正斜杠适用于Windows。
As you see, using forward slashes works on Windows.
这篇关于如何在JDK7中使用目录通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!