Files.newInputStream creates slow InputStream

java

Solution

If you tested `new FileInputStream` second, you are probably just seeing the effect of cache priming by the operating system. It isn't plausible that Java is causing any significant difference to an I/O-bound process. Try it the other way around, and on a much larger dataset.

Problem

On my Windows 7 Files.newInputStream returns sun.nio.ch.ChannelInputStream. When I tested its performance vs FileInputStream I was surprised to know that FileInputStream is faster. This test ``` InputStream in = new FileInputStream("test"); long t0 = System.currentTimeMillis(); byte[] a = new byte[16 * 1024]; for (int n; (n = in.read(a)) != -1;) { } System.out.println(System.currentTimeMillis() - t0); ``` reads 100mb file in 125 ms. If I replace the first line with ``` InputStream in = Files.newInputStream(Paths.get("test")); ``` I get 320ms. If Files.newInputStream is slower what advantages it has over FileInputStream?

Original source