4 Replies Latest reply: Sep 3, 2010 4:21 PM by 796447 RSS

    Access denied error --  while copying file to a specific directory

    843789
      Hi Friends,
      I have gone through scores of examples and i am failing to understand the right thing to be done to copy a file from one directory to another. Here is my class
      package ZipTest;
      
      import java.io.File;
      import java.io.FileInputStream;
      import java.io.FileOutputStream;
      import java.io.IOException;
      
      public class TestCopy {
      
           /**
            * @param args
            */
           public static void main(String[] args) {
                // TODO Auto-generated method stub
                File source = new File("C:\\mkyong\\test_1.txt"); 
                File desc = new File("C:\\GetMe1"); 
                try {
                     copyFiles(source,desc);
                } catch (IOException e) {
                     // TODO Auto-generated catch block
                     e.printStackTrace();
                }
                
      
           }
           
           
           public static void copyFiles(File src, File dest) throws IOException 
           {
           if (!src.exists()) 
           {
           throw new IOException("copyFiles: Can not find source: " + src.getAbsolutePath()+".");
           }
           else if (!src.canRead()) 
           { 
           throw new IOException("copyFiles: No right to source: " + src.getAbsolutePath()+".");
           }
      
           if (src.isDirectory()) 
           {
           if (!dest.exists()) 
           { 
           if (!dest.mkdirs()) 
           {
           throw new IOException("copyFiles: Could not create direcotry: " + dest.getAbsolutePath() + ".");
           }
           }
      
           String list[] = src.list();
      
           for (int i = 0; i < list.length; i++)
           {
           File dest1 = new File(dest, list);
           File src1 = new File(src, list[i]);
           copyFiles(src1 , dest1);
           }
           }
           else
           {
           FileInputStream fin = null;
           FileOutputStream fout = null;
           byte[] buffer = new byte[4096];
           int bytesRead;
           try
           {

           fin = new FileInputStream(src);
           fout = new FileOutputStream (dest);

           while ((bytesRead = fin.read(buffer)) >= 0)
           {
           fout.write(buffer,0,bytesRead);
           }
           }
           catch (IOException e)
           {
           IOException wrapper = new IOException("copyFiles: Unable to copy file: " +
           src.getAbsolutePath() + "to" + dest.getAbsolutePath()+".");
           wrapper.initCause(e);
           wrapper.setStackTrace(e.getStackTrace());
           throw wrapper;
           }
           finally
           {
           if (fin != null) { fin.close(); }
           if (fout != null) { fin.close(); }
           }
           }
           
      }
      But i am getting the following error
      java.io.IOException: copyFiles: Unable to copy file: C:\mkyong\test_1.txttoC:\GetMe1.
           at java.io.FileOutputStream.open(Native Method)
           at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
           at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
           at ZipTest.TestCopy.copyFiles(TestCopy.java:68)
           at ZipTest.TestCopy.main(TestCopy.java:18)
      Caused by: java.io.FileNotFoundException: C:\GetMe1 (Access is denied)
           ... 5 more
      It would be really helpful to have your guidance.
      
      Thanks & Regards
      VS                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    
        • 1. Re: Access denied error --  while copying file to a specific directory
          843789
          It looks to me like you are trying to open a FileOutpuStream on a directory.
           dest.mkDirs();  // creates a directory
           
           fout = new FileOutputStream (dest);   // <-- seems wrong to me
          • 2. Re: Access denied error --  while copying file to a specific directory
            800602
            I copy/paste your code and execute it. I doesn't had any problem with it...
            • 3. Re: Access denied error --  while copying file to a specific directory
              796447
              vishnuS1984 wrote:
              Hi Friends,
              I have gone through scores of examples and i am failing to understand the right thing to be done to copy a file from one directory to another. Here is my class...
              So let's see... C:\GetMe1 is a directory on your machine, right? And this is what you are doing with that directory:
              public static void copyFiles(File src, File dest) throws IOException
              // dest is a 'File' object but represents the C:\GetMe1 directory, right?
              ...
              fout = new FileOutputStream (dest);
              If it's a directory, where in your code are you appending the source file name to the path, before trying to open an output stream on it? You're not.

              BTW, this is awful:
              catch (IOException e)
              {
              IOException wrapper = new IOException("copyFiles: Unable to copy file: " +
              src.getAbsolutePath() + "to" + dest.getAbsolutePath()+".");
              wrapper.initCause(e);
              wrapper.setStackTrace(e.getStackTrace());
              throw wrapper;
              }
              1) You're hiding the original IOException and replacing it with your own? For what good purpose?
              2) Even if you had a good reason to do that, this would be simpler and better:
              throw new IOException("your custom message goes here", e);
              rather than explicitly invokign initCause and setStackTrace. Yuck!
              • 4. Re: Access denied error --  while copying file to a specific directory
                796447
                yvw wrote:
                I copy/paste your code and execute it. I doesn't had any problem with it...
                Right, because you don't have a C:\GetMe1 directory on your machine. So his code copied the source file to a desination file of that exact name in your root directory. That's not representative of the problem.