title: File copy access denied
body: <p>Here I am trying to copy a file one location to another. I am having the source address as</p>
  
  <pre><code>F:\C#\Studies\OS\ch4.ppt
  </code></pre>
  
  <p>where I want to copy ch3.ppt to another location where user chooses .ie., fetching the destination address from the JFilechooser(Where ever the user wants to copy). Here I am getting access denied error</p>
  
  <pre><code>    {
          //Getting the value of fileLocationSourceDrive from table.
          //fileLocationSourceDrive contains F:\C#\Studies\OS\ch4.ppt
  
          JFileChooser location = new JFileChooser();
          location.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          location.showSaveDialog(null);
          String fileLocationDestination = location.getSelectedFile().getAbsolutePath().toString();
          copyFile(fileLocationSourceDrive, fileLocationDestination);
      }
  
  
      public static void copyFile(String sourceFileName, String destionFileName) {
          try {
              System.out.println("Reading..." + sourceFileName);
              File sourceFile = new File(sourceFileName);
              File destinationFile = new File(destionFileName);
              InputStream in = new FileInputStream(sourceFile);
              JOptionPane.showMessageDialog(null, destinationFile);
              OutputStream out = new FileOutputStream(destinationFile);
              //OutputStream out = new FileOutputStream("C:\\Users\\Sagar Ch\\Documents\\New folder");
  
              byte[] buffer = new byte[1024];
              int length;
              while ((length = in.read(buffer)) &gt; 0) {
                  out.write(buffer, 0, length);
              }
              in.close();
              out.close();
              System.out.println("Copied: " + sourceFileName);
          } catch (Exception ex) {
              ex.printStackTrace();
          }
      }
  </code></pre>
  
  <p>Here the error block(Getting access denied)</p>
  
  <pre><code>Reading...F:\C#\Studies\OS\ch4.ppt
  java.io.FileNotFoundException: C:\Users\Sagar Ch\Documents\New folder (Access is denied)
  at java.io.FileOutputStream.open(Native Method)
  at java.io.FileOutputStream.&lt;init&gt;(Unknown Source)
  at java.io.FileOutputStream.&lt;init&gt;(Unknown Source)
  at main.copyFile(main.java:1277)
  at main$22.actionPerformed(main.java:1127)
  at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
  at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
  at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
  at javax.swing.AbstractButton.doClick(Unknown Source)
  at javax.swing.plaf.basic.BasicMenuItemUI.doClick(Unknown Source)
  at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(Unknown Source)
  at java.awt.Component.processMouseEvent(Unknown Source)
  at javax.swing.JComponent.processMouseEvent(Unknown Source)
  at java.awt.Component.processEvent(Unknown Source)
  at java.awt.Container.processEvent(Unknown Source)
  at java.awt.Component.dispatchEventImpl(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
  at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
  at java.awt.Container.dispatchEventImpl(Unknown Source)
  at java.awt.Window.dispatchEventImpl(Unknown Source)
  at java.awt.Component.dispatchEvent(Unknown Source)
  at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
  at java.awt.EventQueue.access$400(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.awt.EventQueue$3.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.awt.EventQueue$4.run(Unknown Source)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
  at java.awt.EventQueue.dispatchEvent(Unknown Source)
  at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
  at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
  at java.awt.EventDispatchThread.run(Unknown Source)
  </code></pre>