Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ignore exception and continue & counting files in a directory

The following snippet of code counts the number of files in a directory:

      Path path = ...;
       ....
      long count = 0;
        try {
           count  = Files.walk(path).parallel().filter(p -> !Files.isDirectory(p)).count();

        } catch (IOException ex) {
            System.out.println(ex.getMessage());
        }

The code above fails to get the number of files, if an exception is thrown.

The question is: How do I ignore the exception and continue counting the number of files.

In Java 7: I have used Files.walkFileTree(path, utils), with the following class:

 public class Utils extends SimpleFileVisitor<Path> {

    private long count;

    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
        if (attrs.isRegularFile()) {
            count++;

        }
        return CONTINUE;
    }

    @Override
    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
        System.err.println(file.getFileName());
        return CONTINUE;
    }

    public long countFilesInDirectoryJava7() {
        return count;
    }

}

Edit: Here is the stack trace of the exception:

  Exception in thread "main" java.io.UncheckedIOException: java.nio.file.AccessDeniedException: E:\8431c36f5b6a3d7169de9cc70a\1025
    at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:88)
    at java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:104)
    at java.util.Spliterators$IteratorSpliterator.trySplit(Spliterators.java:1784)
    at java.util.stream.AbstractTask.compute(AbstractTask.java:297)
    at java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:731)
    at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289)
    at java.util.concurrent.ForkJoinTask.doInvoke(ForkJoinTask.java:401)
    at java.util.concurrent.ForkJoinTask.invoke(ForkJoinTask.java:734)
    at java.util.stream.ReduceOps$ReduceOp.evaluateParallel(ReduceOps.java:714)
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:233)
    at java.util.stream.LongPipeline.reduce(LongPipeline.java:438)
    at java.util.stream.LongPipeline.sum(LongPipeline.java:396)
    at Utils.countFilesInDirectoryJava8(Utils.java:47)
    at TestPath.main(TestPath.java:27)
Caused by: java.nio.file.AccessDeniedException: E:\8431c36f5b6a3d7169de9cc70a\1025
    at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97)
    at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102)
    at sun.nio.fs.WindowsDirectoryStream.<init>(WindowsDirectoryStream.java:86)
    at sun.nio.fs.WindowsFileSystemProvider.newDirectoryStream(WindowsFileSystemProvider.java:518)
    at java.nio.file.Files.newDirectoryStream(Files.java:457)
    at java.nio.file.FileTreeWalker.visit(FileTreeWalker.java:300)
    at java.nio.file.FileTreeWalker.next(FileTreeWalker.java:372)
    at java.nio.file.FileTreeIterator.fetchNextIfNeeded(FileTreeIterator.java:84)
    ... 13 more
Java Result: 1
like image 357
Kachna Avatar asked Sep 05 '15 11:09

Kachna


People also ask

How do you continue after throwing an exception?

Resuming the program When a checked/compile time exception occurs you can resume the program by handling it using try-catch blocks. Using these you can display your own message or display the exception message after execution of the complete program.

How do I ignore an exception?

There is no way to basically ignore a thrown exception. The best that you can do is to limit the standard you have to wrap the exception-throwing code in.

What are the 3 types of exceptions?

There are three types of exception—the checked exception, the error and the runtime exception.

How do you continue if exception occurs in Python?

If an exception occurs during execution of the try clause, the rest of the clause is skipped. Then, if its type matches the exception named after the except keyword, the except clause is executed, and then execution continues after the try/except block.


1 Answers

So far, it seems Files.walk still does not play well with streams. Instead, you might want to use Files.newDirectoryStream instead. Here's some code snippet that might help.

    static class FN<T extends Path> implements Function<T, Stream<T>> {
    @Override
    public Stream<T> apply(T p) {
        if (!Files.isDirectory(p, LinkOption.NOFOLLOW_LINKS)) {
            return Stream.of(p);
        } else {
            try {
                return toStream(Files.newDirectoryStream(p)).flatMap(q -> apply((T) q));
            } catch (IOException ex) {
                return Stream.empty();
            }
        }
    }
}

public static void main(String[] args) throws IOException {
    Path path = Paths.get("some path");
    long count = toStream(Files.newDirectoryStream(path))
            .parallel()
            .flatMap(new FN<>()::apply)
            .count();             

    System.out.println("count: " + count);
}

static <T> Stream<T> toStream(Iterable<T> iterable) {
    return StreamSupport.stream(iterable.spliterator(), false);
}
like image 62
rrufai Avatar answered Oct 06 '22 21:10

rrufai