Java Glob模式用于搜索目录、目录中的文件
我使用glob模式递归地遍历一个目录,并获取该目录中的所有文件和目录 我有一个目录:d:/test/newfolder/。。。 在newfolder中有一些文件和一些目录,它们内部包含一些文件。 例如:d:/test/newfolder/new.txt、d:/test/newfolder/temp/tempfile.doc等 我使用路径Mathcher来匹配全局模式 这是我的密码:Java Glob模式用于搜索目录、目录中的文件,java,Java,我使用glob模式递归地遍历一个目录,并获取该目录中的所有文件和目录 我有一个目录:d:/test/newfolder/。。。 在newfolder中有一些文件和一些目录,它们内部包含一些文件。 例如:d:/test/newfolder/new.txt、d:/test/newfolder/temp/tempfile.doc等 我使用路径Mathcher来匹配全局模式 这是我的密码: PathMatcher matcher = FileSystems.getDefault().getPathMat
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:" + pattern);
try {
Files.walkFileTree(startDir, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult preVisitDirectory(Path dir,BasicFileAttributes attrs) throws IOException {
if (matcher.matches(file.getFileName())) {
System.out.println("found files&directories: " + file.getFileName());
return FileVisitResult.CONTINUE;
}
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (matcher.matches(file.getFileName())) {
System.out.println("found files: " + file.getFileName());
}
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
PathMatcher matcher=FileSystems.getDefault().getPathMatcher(“glob:+pattern”);
试一试{
walkFileTree(startDir,新的SimpleFileVisitor(){
@凌驾
公共文件VisitResult preVisitDirectory(路径目录,基本文件属性属性属性)引发IOException{
if(matcher.matches(file.getFileName())){
System.out.println(“找到的文件和目录:+file.getFileName());
返回FileVisitResult.CONTINUE;
}
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性属性属性)引发IOException{
if(matcher.matches(file.getFileName())){
System.out.println(“找到的文件:+file.getFileName());
}
返回FileVisitResult.CONTINUE;
}
});
}捕获(IOE异常){
e、 printStackTrace();
}
如果我想搜索一个文件,我可以给出glob:.txt模式,它可以正常工作
但我不知道我应该用什么模式来搜索目录。我想得到“newfolder”目录中的所有文件和目录
我正在尝试glob:newfolder/**,但它对我不起作用
代码有什么问题吗
请帮帮我。
提前谢谢有人能回答我的问题吗?我真的需要一个答案。这真的很紧急。:(有人能回答我的问题吗?我真的需要一个答案。这真的很紧急。:)(