Files.walk 跳过目录 [英] Files.walk skip directories

查看:54
本文介绍了Files.walk 跳过目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个程序,在我的外部高清中递归地列出列表文件,但有一个我无权访问的回收站文件夹.我想跳过该文件夹,但似乎无法跳过.

I am trying to write a program the list files recursively in my external hd but there is this recycle bin folder that I don't have access to. I want to skip the folder but can't seem to do it.

下面这段代码有什么问题吗?

Is there anything wrong with this code below?

public static void main(String[] args) throws Exception
{
    String path = "K:\\";

    Files.walk(Paths.get(path))
            .filter(it -> !it.toString().startsWith("K:\\$RECYCLE.BIN"))
            .filter(Files::isRegularFile)
            .forEach(System.out::println);
}

它一直给我这个错误:

Exception in thread "main" java.io.UncheckedIOException java.nio.file.AccessDeniedException: K:\$RECYCLE.BIN\S-1-5-21-684815243-3314879918-332412784-1001
    at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
    at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
    ...

推荐答案

您的代码没有问题,这是 Files.walk 的设计问题.有关详细信息,请参阅此答案.

There is nothing wrong with your code, it's a design issue with Files.walk. See this answer for details.

这篇关于Files.walk 跳过目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆