在使用Files.walk API的java8中,无法使用绝对路径从文件夹中读取文件

Mur*_*uru 1 java windows

我正在尝试使用Files.walk api和java8中的相关路径来读取文件列表,而我正在获取java.nio.file.NoSuchFileException:\ src \ main \ resources \ eclipseconftemplates \ java,相同的api正在使用实际路径

获取异常。

try (Stream<Path> paths = Files.walk(Paths.get("/src/main/resources/eclipseconftemplates/java"))) {
            paths
                .filter(Files::isRegularFile)
                .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
Run Code Online (Sandbox Code Playgroud)

工作正常:

try (Stream<Path> paths = Files.walk(Paths.get("C:\\Users\\XXXX\\maven_project_demo\\antToMaven\\src\\main\\resources\\eclipseconftemplates\\java"))) {
            paths
                .filter(Files::isRegularFile)
                .forEach(System.out::println);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
Run Code Online (Sandbox Code Playgroud)

在此先感谢您的帮助。

Den*_*nis 5

/的目录中不应带有前缀,否则程序将从文件系统的根目录开始。

try (Stream<Path> paths = Files.walk(Paths.get("src/main/resources/eclipseconftemplates/java"))) {
Run Code Online (Sandbox Code Playgroud)