Java 如何在JDK7中使用目录globbing
我一直在尝试在JDK7中使用新的globbing特性,从 我可以让诸如Java 如何在JDK7中使用目录globbing,java,java-7,glob,java.nio.file,Java,Java 7,Glob,Java.nio.file,我一直在尝试在JDK7中使用新的globbing特性,从 我可以让诸如“glob:.dat”之类的glob与 Files.walkFileTree(startingDir, finder); 示例,但我无法使“**”语法正常工作。我希望能够创建如下内容: matcher = FileSystems.getDefault().getPathMatcher("glob:" + "foo/**/bar/*.dat"); 如果能举个简单的例子,我将不胜感激。我正在使用Windows7 更新: @Ol
“glob:.dat”
之类的glob与
Files.walkFileTree(startingDir, finder);
示例,但我无法使“**”
语法正常工作。我希望能够创建如下内容:
matcher = FileSystems.getDefault().getPathMatcher("glob:" + "foo/**/bar/*.dat");
如果能举个简单的例子,我将不胜感激。我正在使用Windows7
更新:
@Oleg和@JBNizet明确指出“/”语法与操作系统无关。注意,建议也可以使用依赖操作系统的语法(?必需)
仍然存在的问题:
已采取@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;
}
});
}
但是没有匹配的证据这里有一个工作示例,它显示了
d://
的任何子目录中的所有zip文件:
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;
}
});
}
publicstaticvoidmain(字符串[]args)引发IOException{
final-PathMatcher-matcher=FileSystems.getDefault().getPathMatcher(“glob:d://***.zip”);
Files.walkFileTree(path.get(“d:/”),新的SimpleFileVisitor(){
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性属性属性)引发IOException{
if(matcher.matches(文件)){
System.out.println(文件);
}
返回FileVisitResult.CONTINUE;
}
@凌驾
公共文件VisitResult visitFileFailed(路径文件,IOException exc)引发IOException{
返回FileVisitResult.CONTINUE;
}
});
}
正如您所看到的,在Windows上使用前斜杠是可行的。您需要使用**开始glob
matcher = FileSystems.getDefault().getPathMatcher("glob:**/foo/**/bar/*.dat");
否则,打电话
matcher.matches(file)
尝试将文件的完整路径与以相对路径(/foo/)而不是绝对路径(d:/petermr workspace/../foo)开头的正则表达式匹配
将**前置到glob只是告诉它忽略绝对路径的开头。
/
在任何Windows系统上都可以正常工作,如果这是您的系统concern@Oleg谢谢所以我有一些逻辑错误,或者假设这些方法有不同的功能。File.separator是否因此错误?[我发现官方文档相当粗略]您尝试过“glob:*/*.cml”吗?@Kristof谢谢-似乎您必须使用**
组件谢谢。它会检查相关的文件路径吗?我们已经接受了这一点,因为这是唯一的答案,它解决了文件分隔符的问题。但我无法让它在我的环境中工作。globbing语法的文档记录似乎比较少,也没有明确的示例。哦,很好,明天可能会起作用……如果您甚至想删除以下特定于驱动器的使用:glob://**.zip
可以使用模式glob:*/*.zip
删除特定于驱动器的部分,甚至扩展名glob:*/*.
Ummm。。。。这是可行的,但效率不高。如果您的模式是glob:/foo/bar/baz/***.zip
,那么它将遍历整个文件树,而不仅仅是/foo/bar/baz
组件。
matcher.matches(file)