title: FileChannel.transferTo for large file in windows
body: <p>Using Java NIO use can copy file faster. I found two kind of method mainly over internet to do this job.</p>
  <pre><code>public static void copyFile(File sourceFile, File destinationFile) throws IOException {
      if (!destinationFile.exists()) {
          destinationFile.createNewFile();
      }
  
      FileChannel source = null;
      FileChannel destination = null;
      try {
          source = new FileInputStream(sourceFile).getChannel();
          destination = new FileOutputStream(destinationFile).getChannel();
          destination.transferFrom(source, 0, source.size());
      } finally {
          if (source != null) {
              source.close();
          }
          if (destination != null) {
              destination.close();
          }
      }
  }
  </code></pre>
  <p>In <a href="http://viralpatel.net/blogs/2009/05/20-useful-java-code-snippets-for-java-developers.html" rel="noreferrer">20 very useful Java code snippets for Java Developers</a> I found a different comment and trick:</p>
  <pre><code>public static void fileCopy(File in, File out) throws IOException {
      FileChannel inChannel = new FileInputStream(in).getChannel();
      FileChannel outChannel = new FileOutputStream(out).getChannel();
      try {
          // inChannel.transferTo(0, inChannel.size(), outChannel); // original -- apparently has trouble copying large files on Windows
          // magic number for Windows, (64Mb - 32Kb)
          int maxCount = (64 * 1024 * 1024) - (32 * 1024);
          long size = inChannel.size();
          long position = 0;
          while (position &lt; size) {
              position += inChannel.transferTo(position, maxCount, outChannel);
          }
      } finally {
          if (inChannel != null) {
              inChannel.close();
          }
          if (outChannel != null) {
              outChannel.close();
          }
      }
  }
  </code></pre>
  <p>But I didn't find or understand what is meaning of</p>
  <blockquote>
  <p>&quot;magic number for Windows, (64Mb - 32Kb)&quot;</p>
  </blockquote>
  <p>It says that <code>inChannel.transferTo(0, inChannel.size(), outChannel)</code> has problem in windows, is 32768 (= (64 * 1024 * 1024) - (32 * 1024)) byte is optimum for this method.</p>