如何在 JDK7 中使用目录通配
2022-09-02 10:17:38
我一直在尝试使用JDK7中新的通配功能,从文档和示例开始
我可以得到球,如与"glob:*.dat"
Files.walkFileTree(startingDir, finder);
示例,但我一直无法使语法正常工作。我希望能够创建如下内容:"**"
matcher = FileSystems.getDefault().getPathMatcher("glob:" + "foo/**/bar/*.dat");
如果能举一个简单的例子,我们将不胜感激。我使用的是 Windows 7。
更新:@Oleg和@JBNizet清楚地表明“/”语法与操作系统无关。请注意,Javadocs建议依赖于操作系统的语法也是可能的(?必需的)
仍然存在问题:已采取@Nizet并按如下方式编辑:
@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
但没有匹配的证据