title: Why does nio.files.copy is a lot slower than nio.fileChannel copy?
body: <p>I'm beginner Java programmer.<br>
  Today, I practiced how to copy file in java and tried to follow this tutorial 
  <a href="http://www.journaldev.com/861/4-ways-to-copy-file-in-java" rel="nofollow">http://www.journaldev.com/861/4-ways-to-copy-file-in-java</a><br>
  Having finished this tutorial, I ran the JMH Benchmark to check the performance, with a 57MB txt file.<br>
  And there is a performance gap between nioFiles and NIOChannel, larger than I expected.<br></p>
  
  <pre><code>Benchmark                                   Mode  Cnt   Score   Error  Units
  CompressTest.fileCopyUsingNIOChannelClass  thrpt   10  22.465 ± 2.996  ops/s
  CompressTest.fileCopyWithNIOFiles          thrpt   10   0.843 ± 0.488  ops/s
  </code></pre>
  
  <p>Here is a code I used<br></p>
  
  <pre><code>    @Warmup(iterations = 5, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
      @Measurement(iterations = 10, time = 1000, timeUnit = TimeUnit.MILLISECONDS)
      @Fork(1)
      @State(Scope.Benchmark)
      public class CompressTest
      {
      final static Path source = Paths.get("c:/temp/system.out.lambda.txt");
      final static Path target = Paths.get("c:/temp/copied.lambda.txt");
  
      public static void main(String[] args) throws RunnerException, IOException {
          Main.main(args);
      }
  
      @Benchmark
      public static void fileCopyWithNIOFiles() throws IOException{
          Files.copy(source, target, StandardCopyOption.REPLACE_EXISTING);
      }
      @Benchmark
      public static void fileCopyUsingNIOChannelClass() throws IOException
          {
              File fileToCopy = new File("c:/temp/system.out.lambda.txt");
              FileInputStream inputStream = new FileInputStream(fileToCopy);
              FileChannel inChannel = inputStream.getChannel();
  
              File newFile = new File("c:/temp/testcopied.txt");
              FileOutputStream outputStream = new FileOutputStream(newFile);
              FileChannel outChannel = outputStream.getChannel();
  
              inChannel.transferTo(0, fileToCopy.length(), outChannel);
              inputStream.close();
              outputStream.close();
          }
  }
  </code></pre>
  
  <p>So I want to ask, did I do something wrong? or could you explain why this happend? </p>
  
  <hr>
  
  <p>Somebody asked, so I tried over the other file. 348MB avi file and following is the result.</p>
  
  <pre><code>Benchmark                                   Mode  Cnt  Score   Error  Units
  CompressTest.fileCopyUsingNIOChannelClass  thrpt   10  3.142 ± 0.738  ops/s
  CompressTest.fileCopyWithNIOFiles          thrpt   10  1.991 ± 0.350  ops/s
  </code></pre>
  
  <p>Still file copy with a nioFile is slower than using NIOChannel.</p>
  
  <hr>
  
  <p>I refreshed everything and just tested once more and this is the reuslt. with a 57MB txt file and set warmup iterations as 10.</p>
  
  <pre><code>Benchmark                                   Mode  Cnt   Score   Error  Units
  CompressTest.fileCopyUsingNIOChannelClass  thrpt   10  23.442 ± 3.224  ops/s
  CompressTest.fileCopyWithNIOFiles          thrpt   10  12.328 ± 2.128  ops/s
  </code></pre>
  
  <p>This result is more acceptable than a first one, but still the speed of nioFile copying is nearly half of the NIOChannel.</p>
  
  <p>P.S. :  if there is a better way to copy a file, please tell me. I really want to learn more about the java, thanks.</p>