问题
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