当前位置: 首页 > 工具软件 > globbing > 使用案例 >

java中如何使用jdk_java – 如何在JDK7中使用目录globbing

令狐新翰
2023-12-01

the documentation and examples开始,我一直试图在JDK7中使用新的全局功能

我可以得到globs,如“glob:*.dat”来处理

Files.walkFileTree(startingDir, finder);

示例,但我无法获得“**”语法工作.我想要创建一些像:

matcher = FileSystems.getDefault().getPathMatcher("glob:" + "foo/**/bar/*.dat");

并将感激一个简单的例子.我正在使用Windows 7.

更新:

@Oleg和@JBNizet清楚地表明“/”语法与OS无关.请注意,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() {

@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

但没有匹配的证据

 类似资料: