4

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)
    ...
Satana Lee
  • 53
  • 1
  • 1
  • 4

1 Answers1

2

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

Community
  • 1
  • 1
D.B.
  • 4,523
  • 2
  • 19
  • 39
  • oh ok so I guess I have to do it the old fashion way of recursive functions? – Satana Lee Nov 17 '16 at 01:42
  • No, not necessarily. There are several ways you could walk the file tree. For example you could use `FileVisitor` as [described here](https://docs.oracle.com/javase/tutorial/essential/io/walk.html) – D.B. Nov 17 '16 at 01:46