Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8040828

Files.walk should support a way to ignore exceptions

    Details

    • Type: Enhancement
    • Status: Closed
    • Priority: P4
    • Resolution: Duplicate
    • Affects Version/s: 8
    • Fix Version/s: None
    • Component/s: core-libs
    • Labels:
    • Subcomponent:
    • CPU:
      x86_64
    • OS:
      windows_7

      Description

      A DESCRIPTION OF THE REQUEST :
      See question on SO:

      http://stackoverflow.com/questions/22867286/files-walk-calculate-total-size

      When walking over some files using Files.walk(), some files can't be visited
      and we will get an exception:
      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)


      JUSTIFICATION :
      Comment from SO:
      "I believe this is a design failure, because as it stands now Files.walk is absolutely unusable, because you never can guarantee that there will be no errors when walking over a directory."

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      One proposal from SO:

      1. "I believe it can still be fixed, though I do not know how FileVisitOptions exactly work.
      Currently there is a FileVisitOption.FOLLOW_LINKS, if it operates on a per file basis, then I would suspect that a FileVisitOption.IGNORE_ON_IOEXCEPTION could also be added, however we cannot correctly inject that functionality in there."

      and my own:
      2: I think i would prefer maybe another Files.walk() that also accepted an errorHandler or similiar.

      ---------- BEGIN SOURCE ----------
      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;
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      Use the old java-7 Files.walkFileTree api and implement an anonymous visitor class.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: