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

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

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

问题

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

try {
  Files.walkFileTree(Utils.getContentDirectory().toPath(), new SimpleFileVisitor<Path>() { //**此处异常**

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                
      Utils.log(file.toString());    
      return FileVisitResult.CONTINUE;

    }  
  });
} catch (IOException e) {
  e.printStackTrace();
}

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

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

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

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

try {
  Files.walkFileTree(Utils.getContentDirectory().toPath(), new SimpleFileVisitor&lt;Path&gt;() { //**Exception here**

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                
      Utils.log(file.toString());    
      return FileVisitResult.CONTINUE;

    }  
  });
} catch (IOException e) {
  e.printStackTrace();
}

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

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

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

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

public static void main(String[] args) throws IOException
{
    var jar = Path.of("C:\\Users\\raz\\Desktop\\WebServer.jar");

    System.out.println("isRegularFile()=" + Files.isRegularFile(jar));

    FileVisitor<? super Path> visitor = new FileVisitor<Path>()
    {
        public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
        {
            System.out.println("dir START " + dir);
            return FileVisitResult.CONTINUE;
        }
        public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
        {
            System.out.println("file " + file);
            return FileVisitResult.CONTINUE;
        }
        public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
        {
            System.out.println("file fail " + file);
            return FileVisitResult.CONTINUE;
        }
        public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
        {
            System.out.println("dir END " + dir);
            return FileVisitResult.CONTINUE;
        }
    };

    try (FileSystem fs = FileSystems.newFileSystem(jar))
    {
        Path path = fs.getPath("/resources/html");

        System.out.println("walkFileTree within archive " + jar + " starting at path: " + path);

        Files.walkFileTree(path, visitor);
    }

    Path parent = jar.getParent();
    System.out.println("walkFileTree at " + parent);
    Files.walkFileTree(parent, visitor);
}
英文:

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:

public static void main(String[] args) throws IOException
{
var jar = Path.of(&quot;C:\\Users\\raz\\Desktop\\WebServer.jar&quot;);
System.out.println(&quot;isRegularFile()=&quot;+Files.isRegularFile(jar));
FileVisitor&lt;? super Path&gt; visitor = new FileVisitor&lt;Path&gt;()
{
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException
{
System.out.println(&quot;dir START &quot;+dir);
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException
{
System.out.println(&quot;file &quot;+file);
return FileVisitResult.CONTINUE;
}
public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException
{
System.out.println(&quot;file fail &quot;+file);
return FileVisitResult.CONTINUE;
}
public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException
{
System.out.println(&quot;dir END &quot;+dir);
return FileVisitResult.CONTINUE;
}
};
try (FileSystem fs = FileSystems.newFileSystem(jar))
{
Path path = fs.getPath(&quot;/resources/html&quot;);
System.out.println(&quot;walkFileTree within archive &quot;+ jar+&quot; starting at path: &quot;+path);
Files.walkFileTree(path, visitor);
}
Path parent = jar.getParent();
System.out.println(&quot;walkFileTree at &quot;+ parent);
Files.walkFileTree(parent, visitor);
}

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:

确定