Files.walk skip directories [duplicate]

一笑奈何 提交于 2021-01-27 07:14:53

问题


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);
}

It keeps giving me this error:

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)
    ...

回答1:


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



来源:https://stackoverflow.com/questions/40644979/files-walk-skip-directories

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!