当我获取java.nio.file.NoSuchFileException时,我试图使用Files.walk api读取具有相关路径的文件列表:\ 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();
}
工作正常:
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();
}
预先感谢您的帮助。
答案 0 :(得分:5)
您的目录中不应包含前缀/
,否则程序将从文件系统的根目录开始。
try (Stream<Path> paths = Files.walk(Paths.get("src/main/resources/eclipseconftemplates/java"))) {
答案 1 :(得分:2)
由于系统无法解析您所引用的文件路径,因此出现错误。 文件分隔符取决于系统。在UNIX上为“ /”,在Windows上为“ \”。 如果要独立于平台工作,请始终使用System Properties。
System.getProperty("file.separator");