title: Files.walk(), calculate total size
body: <p>I'm trying to calculate the size of the files on my disc. In java-7 this could be done using <a href="http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#walkFileTree-java.nio.file.Path-java.nio.file.FileVisitor-" rel="noreferrer">Files.walkFileTree</a> as shown in my answer <a href="https://stackoverflow.com/questions/7255592/get-file-directory-size-using-java-7-new-io/19869323#comment32024328_19869323">here</a>.</p>
  
  <p>However if i wanted to do this using java-8 streams it will work for some folders, but not for all.</p>
  
  <pre><code>public static void main(String[] args) throws IOException {
      long size = Files.walk(Paths.get("c:/")).mapToLong(MyMain::count).sum();
      System.out.println("size=" + size);
  }
  
  static long count(Path path) {
      try {
          return Files.size(path);
      } catch (IOException | UncheckedIOException e) {
          return 0;
      }
  }
  </code></pre>
  
  <p>Above code will work well for path <code>a:/files/</code> but for <code>c:/</code> it will throw below exception</p>
  
  <pre><code>Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: c:\$Recycle.Bin\S-1-5-20
  at java.nio.file.FileTreeIterator.fetchNextIfNeeded(Unknown Source)
  at java.nio.file.FileTreeIterator.hasNext(Unknown Source)
  at java.util.Iterator.forEachRemaining(Unknown Source)
  at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Unknown Source)
  at java.util.stream.AbstractPipeline.copyInto(Unknown Source)
  at java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
  at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
  at java.util.stream.AbstractPipeline.evaluate(Unknown Source)
  at java.util.stream.LongPipeline.reduce(Unknown Source)
  at java.util.stream.LongPipeline.sum(Unknown Source)
  at MyMain.main(MyMain.java:16)
  </code></pre>
  
  <p>I understand where it is coming from and how to avoid it using Files.walkFileTree API.</p>
  
  <p>But how can this exception be avoided using <a href="http://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#walk-java.nio.file.Path-java.nio.file.FileVisitOption...-" rel="noreferrer">Files.walk()</a> API?</p>