Java NIO file path issue

You need to convert the found resource to URI. It works on all platforms and protects you from possible errors with paths. You must not worry about how full path looks like, whether it starts with ‘\’ or other symbols. If you think about such details – you do something wrong. ClassLoader classloader = Thread.currentThread().getContextClassLoader(); … Read more

Wrapping a ByteBuffer with an InputStream

There seem to be some bugs with the implementation referred to by Thilo, and also copy and pasted on other sites verbatim: ByteBufferBackedInputStream.read() returns a sign extended int representation of the byte it reads, which is wrong (value should be in range [-1..255]) ByteBufferBackedInputStream.read(byte[], int, int) does not return -1 when there are no bytes … Read more

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

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