title: fileoutputstream in Java generating zero byte if source destination are same?
body: <p>We are using filecopy in Java using similar code:</p>
  
  <pre><code>private void copyFileUsingFileChannels(File source, File dest) throws IOException {
  
      FileChannel inputChannel = null;
      FileChannel outputChannel = null;
  
      try {
          inputChannel = new FileInputStream(source).getChannel();
          outputChannel = new FileOutputStream(dest).getChannel();
          outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
      } finally {
          inputChannel.close();
          outputChannel.close();
      }
  }
  </code></pre>
  
  <p>The problem is sometimes <code>source</code> and <code>dest</code> can point to the same file, in that case, the following statement, <code>outputChannel = new FileOutputStream(dest).getChannel();</code> is causing the source to truncate to 0 bytes even if source is 400 kb till that point, as I think it is opening a stream for write with same handle. So what is the way to counter this?</p>
  
  <p>Should I add something in my code saying</p>
  
  <pre><code>if (! (sourcec.getAbsolutePath().equalsIgnoreCase(destinationc.getAbsolutePath()))) 
      copyFiles(sourcec, destinationc);
  </code></pre>
  
  <p>Would the above work? Or is there a better way to handle this?</p>
  
  <p>Thanks </p>