title: Copying to existing set of destination file/directory using java.nio package results in java.nio.file.FileAlreadyExistsException
body: <p>I am attempting to copy one file to another location using Java NIO API. When I run below code I get <code>java.nio.file.FileAlreadyExistsException</code>.</p>
  
  <pre><code>public static void copyFileUsingNio(File sourceFile, String destnationFilePath) {
    try {   
      if (sourceFile != null &amp;&amp; destnationFilePath != null) {
        java.nio.file.Path sourcePath = sourceFile.toPath();
        java.nio.file.Path destinationPath = java.nio.file.Paths.get(destnationFilePath);
  
        if (!java.nio.file.Files.exists(destinationPath)) {
          java.nio.file.Path parent = destinationPath.getParent();
          if (parent != null) {
            java.nio.file.Files.createDirectories(destinationPath.getParent());
            java.nio.file.Files.createFile(destinationPath);    
          }
          destinationPath = java.nio.file.Files.copy(sourcePath, destinationPath);
        } else {
          destinationPath = java.nio.file.Files.copy(
              sourcePath, destinationPath,
              java.nio.file.StandardCopyOption.REPLACE_EXISTING);
        }
      }
    } catch(Exception e) {
      e.printStackTrace();
    }
  }
  </code></pre>
  
  <p>StackTrace :</p>
  
  <blockquote>
    <p>java.nio.file.FileAlreadyExistsException :
    C:\Users\guest\Desktop\Files\desttest.txt</p>
    
    <p>at sun.nio.fs.WindowsFileCopy.copy(Unknown Source) 
    at sun.nio.fs.WindowsFileSystemProvider.copy(Unknown Source) 
    at java.nio.file.Files.copy(Unknown Source) 
    at com.filetest.copyFileUsingNio(TestFile.java:110)</p>
  </blockquote>
  
  <p>Here <code>destnationFilePath</code> is <strong>C:/Users/guest/Desktop/Files/desttest.txt</strong>. Also if file already exists then I need to replace it else copy the file.
  Please advice me.</p>