title: Copy file in java to directory that may not exist
body: <p>I am trying to copy a file to a path that may not exist with this code</p>
  
  <pre><code>    public static void copyFile( File from, File to ) throws IOException {
  
      if ( !to.exists() ) { to.createNewFile(); }
  
      try (
          FileChannel in = new FileInputStream( from ).getChannel();
          FileChannel out = new FileOutputStream( to ).getChannel() ) {
  
          out.transferFrom( in, 0, in.size() );
      }
  </code></pre>
  
  <p>which is obviously wrong because if the directory does not exist it wouldn't copy the file. It needs to create the folders that do not exist in the path.</p>
  
  <p>For example the program should copy a file to:</p>
  
  <p>C:\test\test1\test2\test3\copiedFile.exe</p>
  
  <p>where the directory test in C:\ exists but test2 and test3 are missing, so the program should create them.</p>