Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/401.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我不能';你不能从这些代码中打印任何单词吗?_Java - Fatal编程技术网

Java 为什么我不能';你不能从这些代码中打印任何单词吗?

Java 为什么我不能';你不能从这些代码中打印任何单词吗?,java,Java,为什么我不能从这些代码中打印任何单词? Eclipse在控制台字段上没有显示任何内容 package test; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.PathMatcher; import java.nio.file.Paths; public class Test2 { public static void main(String[] args) {

为什么我不能从这些代码中打印任何单词? Eclipse在控制台字段上没有显示任何内容

package test;

import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.nio.file.PathMatcher;
import java.nio.file.Paths;

public class Test2 {

    public static void main(String[] args) {
        PathMatcher matcher =
                FileSystems.getDefault().getPathMatcher("glob:*.{java,class}");

            Path filename = Paths.get("C:\\Users\\user\\eclipse-workspace\\test\\bin\\test");
            if (matcher.matches(filename)) {
                System.out.println(filename);
            }

    }   
}

您正在匹配目录路径,而不是文件。如果要匹配单个文件路径,可以执行以下操作:

PathMatcher matcher = FileSystems.getDefault()
        .getPathMatcher("glob:**/*.{java,class}");
Path path = Paths.get("..", "classes", "MyClass.class");
if (matcher.matches(path)) {
  System.out.println(path);
}
请看一看,其中显示了如何将matcher与
文件一起使用。walkFileTree()
来检查目录。它或多或少应该包括以下内容:

PathMatcher matcher = FileSystems.getDefault()
        .getPathMatcher("glob:**/*.{java,class}");
Path path = Paths.get("..", "classes");
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (matcher.matches(file)) {
            System.out.println(file);
        }
        return FileVisitResult.CONTINUE;
    }
    ...
PathMatcher matcher=FileSystems.getDefault()
.getPathMatcher(“glob:*/*.{java,class}”);
路径路径=路径。获取(“…”,“类”);
walkFileTree(路径,新的SimpleFileVisitor(){
@凌驾
公共文件VisitResult visitFile(路径文件,基本文件属性属性属性)引发IOException{
if(matcher.matches(文件)){
System.out.println(文件);
}
返回FileVisitResult.CONTINUE;
}
...

因为
匹配器不匹配!?为什么它要打印东西?路径名与
*.java
*.class
不匹配---你是否错误地认为这些代码实际上会扫描硬盘上的文件?如果是,你为什么相信?但我有很多*.class文件在里面“C:\\Users\\user\\eclipse workspace\\test\\bin\\test”@SquallHuang您认为您的代码中的哪一部分会实际扫描硬盘上的文件?您为什么这么认为?您是否阅读了正在使用的方法的javadoc?哪一个会扫描文件?谢谢Karol。我已经理解了“匹配单个文件路径”的方法“。但我不明白如何使用文件.walkFileTree()。我想打印eclipse控制台上“C:\\Users\\user\\eclipse workspace\\test\\bin\\test”目录下的所有*.class文件。