title: How to copy file in java
body: <p>Im trying to copy a file in java and move it to a new folder. This is the code i HAve been using but I always get this error "(Access is denied) in the specified directory". Is there a way i can fix this or a better way to copy the files? thanks</p>
  
  <pre><code>try{
            File f1 = new File(fpath);
            File f2 = new File("C:/users/peter/documents/foldertest2/hats");
            InputStream in = new FileInputStream(f1);
  
            //For Append the file.
            //OutputStream out = new FileOutputStream(f2,true);
  
            //For Overwrite the file.
            OutputStream out = new FileOutputStream(f2);
  
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) &gt; 0){
              out.write(buf, 0, len);
            }
            in.close();
            out.close();
            System.out.println("File copied.");
          }
          catch(FileNotFoundException ex){
            System.out.println(ex.getMessage() + " in the specified directory.");
            System.exit(0);
          }
          catch(IOException e){
            System.out.println(e.getMessage());      
          }
  </code></pre>
  
  <p>UPDATE:
  I checked the folder permissions and they are all open for all users and mine</p>