Avoid Java 8 Files.walk(..) termination cause of ( java.nio.file.AccessDeniedException ) [duplicate]

Answer Here is a temporary solution , which can be improved to use Java 8 Streams and Lambdas. int[] count = {0}; try { Files.walkFileTree( Paths.get(dir.getPath()), new HashSet<FileVisitOption>(Arrays.asList(FileVisitOption.FOLLOW_LINKS)), Integer.MAX_VALUE, new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { System.out.printf(“Visiting file %s\n”, file); ++count[0]; return FileVisitResult.CONTINUE; } @Override public FileVisitResult visitFileFailed(Path file, … Read more

WatchService and SwingWorker: how to do it correctly?

Because your background thread is devoted entirely to watching, take() is the right choice. It effectively hides the platform dependent implementation, which may either forward or poll. One of the poll() methods would be appropriate if, for example, your background thread also needed to examine other queues in series with the WatchService. Addendum: Because the … Read more

Why is Files.list() parallel stream performing so much slower than using Collection.parallelStream()?

The problem is that current implementation of Stream API along with the current implementation of IteratorSpliterator for unknown size source badly splits such sources to parallel tasks. You were lucky having more than 1024 files, otherwise you would have no parallelization benefit at all. Current Stream API implementation takes into account the estimateSize() value returned … Read more

Java: Path vs File

Long story short: java.io.File will most likely never be deprecated / unsupported. That said, java.nio.file.Path is part of the more modern java.nio.file lib, and does everything java.io.File can, but generally in a better way, and more. For new projects, use Path. And if you ever need a File object for legacy, just call Path#toFile() Migrating … Read more

Files.walk(), calculate total size

No, this exception cannot be avoided. The exception itself occurs inside the the lazy fetch of Files.walk(), hence why you are not seeing it early and why there is no way to circumvent it, consider the following code: long size = Files.walk(Paths.get(“C://”)) .peek(System.out::println) .mapToLong(this::count) .sum(); On my system this will print on my computer: C:\ … Read more

Java NIO FileChannel versus FileOutputstream performance / usefulness

My experience with larger files sizes has been that java.nio is faster than java.io. Solidly faster. Like in the >250% range. That said, I am eliminating obvious bottlenecks, which I suggest your micro-benchmark might suffer from. Potential areas for investigating: The buffer size. The algorithm you basically have is copy from disk to buffer copy … Read more