发生错误,尝试递归打印给定目录中的所有文件路径时。

huangapple go评论87阅读模式
英文:

An error occurred when attempting to recursively print all file paths in a given directory

问题

我正试图递归打印目录中的所有文件路径。

  1. try {
  2. Files.walkFileTree(Utils.getContentDirectory().toPath(), new SimpleFileVisitor<Path>() { //**此处异常**
  3. @Override
  4. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  5. Utils.log(file.toString());
  6. return FileVisitResult.CONTINUE;
  7. }
  8. });
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }

这是我正在尝试从中读取的目录,它确实存在(我正在使用Maven,它是一个内部目录)。

  1. public static File getContentDirectory() {
  2. return new File(UltimateBugTracker.class.getClassLoader().getResource("resources/html/index.html").getFile()).getParentFile();
  3. }

但出于某种原因,它会抛出以下异常:

  1. Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\raz\Desktop\WebServer.jar!\resources\html

这不合理,因为我正在使用内置的fileInstance.toPath()方法获取路径。我不明白为什么它会说这是无效的路径。

英文:

I'm trying to recursively print all of the file paths from a directory

  1. try {
  2. Files.walkFileTree(Utils.getContentDirectory().toPath(), new SimpleFileVisitor&lt;Path&gt;() { //**Exception here**
  3. @Override
  4. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
  5. Utils.log(file.toString());
  6. return FileVisitResult.CONTINUE;
  7. }
  8. });
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }

This is the directory I'm trying to read from, which does exist (I'm using maven and it's an internal directory)

  1. public static File getContentDirectory() {
  2. return new File(UltimateBugTracker.class.getClassLoader().getResource(&quot;resources/html/index.html&quot;).getFile()).getParentFile();
  3. }

but for whatever reason it's throwing this exception

> Exception in thread "main" java.nio.file.InvalidPathException: Illegal char <:> at index 4: file:\C:\Users\raz\Desktop\WebServer.jar!\resources\html

which doesn't make sense because I'm using the build in fileInstance.toPath() method to get the path. I don't understand why it is saying that it's an invalid path.

答案1

得分: 1

以下是翻译好的代码部分:

  1. public static void main(String[] args) throws IOException
  2. {
  3. var jar = Path.of("C:\\Users\\raz\\Desktop\\WebServer.jar");
  4. System.out.println("isRegularFile()=" + Files.isRegularFile(jar));
  5. FileVisitor<? super Path> visitor = new FileVisitor<Path>()
  6. {
  7. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
  8. {
  9. System.out.println("dir START " + dir);
  10. return FileVisitResult.CONTINUE;
  11. }
  12. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
  13. {
  14. System.out.println("file " + file);
  15. return FileVisitResult.CONTINUE;
  16. }
  17. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
  18. {
  19. System.out.println("file fail " + file);
  20. return FileVisitResult.CONTINUE;
  21. }
  22. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
  23. {
  24. System.out.println("dir END " + dir);
  25. return FileVisitResult.CONTINUE;
  26. }
  27. };
  28. try (FileSystem fs = FileSystems.newFileSystem(jar))
  29. {
  30. Path path = fs.getPath("/resources/html");
  31. System.out.println("walkFileTree within archive " + jar + " starting at path: " + path);
  32. Files.walkFileTree(path, visitor);
  33. }
  34. Path parent = jar.getParent();
  35. System.out.println("walkFileTree at " + parent);
  36. Files.walkFileTree(parent, visitor);
  37. }
英文:

You are not passing a valid Path to walkFileTree, but a URI for a resource within a jar. Hopefully this example makes it clearer on how to walk the resources within your jar vs files in a directory:

  1. public static void main(String[] args) throws IOException
  2. {
  3. var jar = Path.of(&quot;C:\\Users\\raz\\Desktop\\WebServer.jar&quot;);
  4. System.out.println(&quot;isRegularFile()=&quot;+Files.isRegularFile(jar));
  5. FileVisitor&lt;? super Path&gt; visitor = new FileVisitor&lt;Path&gt;()
  6. {
  7. public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
  8. {
  9. System.out.println(&quot;dir START &quot;+dir);
  10. return FileVisitResult.CONTINUE;
  11. }
  12. public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
  13. {
  14. System.out.println(&quot;file &quot;+file);
  15. return FileVisitResult.CONTINUE;
  16. }
  17. public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
  18. {
  19. System.out.println(&quot;file fail &quot;+file);
  20. return FileVisitResult.CONTINUE;
  21. }
  22. public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
  23. {
  24. System.out.println(&quot;dir END &quot;+dir);
  25. return FileVisitResult.CONTINUE;
  26. }
  27. };
  28. try (FileSystem fs = FileSystems.newFileSystem(jar))
  29. {
  30. Path path = fs.getPath(&quot;/resources/html&quot;);
  31. System.out.println(&quot;walkFileTree within archive &quot;+ jar+&quot; starting at path: &quot;+path);
  32. Files.walkFileTree(path, visitor);
  33. }
  34. Path parent = jar.getParent();
  35. System.out.println(&quot;walkFileTree at &quot;+ parent);
  36. Files.walkFileTree(parent, visitor);
  37. }

huangapple
  • 本文由 发表于 2020年8月19日 01:44:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/63473961.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定