gpt4 book ai didi

java - 为什么我得到这个 InvalidPathException

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:08:02 24 4
gpt4 key购买 nike

这段代码有问题。即我收到消息:

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: [E:\Temp\564\324\123.txt]

public static void main(String[] args) throws Exception{
Path sourseFile = Paths.get("E:\\Temp");
Path[] result = searchFile(sourseFile, "123");
for (Path path : result) {
System.out.println(path);
}
}

public static Path[] searchFile (Path path, String fileName)throws Exception{

DirectoryStream<Path> dirStream = Files.newDirectoryStream(path);
ArrayList<Path> temp = new ArrayList<>();
for (Path s : dirStream) {
if (s.toFile().isDirectory()){
temp.add(Paths.get(Arrays.toString(searchFile(s, fileName))));
}
else {
if (s.toAbsolutePath().toString().contains(fileName)){
temp.add(s.toAbsolutePath());
}
}
}
return temp.toArray(Path[]::new);
}

完整追踪

Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 2: [E:\Temp\564\324\123.txt] at java.base/sun.nio.fs.WindowsPathParser.normalize(WindowsPathParser.java:182) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:153) at java.base/sun.nio.fs.WindowsPathParser.parse(WindowsPathParser.java:77) at java.base/sun.nio.fs.WindowsPath.parse(WindowsPath.java:92) at java.base/sun.nio.fs.WindowsFileSystem.getPath(WindowsFileSystem.java:229) at java.base/java.nio.file.Path.of(Path.java:147) at java.base/java.nio.file.Paths.get(Paths.java:69) at s09.Task1.searchFile(Task1.java:28) at s09.Task1.searchFile(Task1.java:28) at s09.Task1.main(Task1.java:13)

最佳答案

您似乎将字符串“[E:...]”(包括由 Arrays.toString 添加的周围方括号)传递给 Paths.get。这随后被解释为普通文件名,其中“:”字符确实是非法的。

关于java - 为什么我得到这个 InvalidPathException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56736003/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com