Question

My program loops through some folders and copies the files in the folder to a new file with a new name. Some files will copy and others will get a Java Exception saying access denied. When that happens the program terminates. I want it to skip and not copy that file and just keep going. Here is the copy function.

private static void copyFile(File source, File dest)
        throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } catch (Exception e){

    }


        finally{
    inputChannel.close();
    outputChannel.close();
    }
}

Any help would be great. Thanks!

Était-ce utile?

La solution

Just catch the exception in the calling program to your copyFile method and continue on. The reason I removed the catch block in the copyFile method is it allows the copyFile method to be generally used (times when you might want to stop processing during an exception and times when you want to ignore the exception).

...
for (File source : sources) {
   try {
      copyFile(source, dest);
   }
   catch (Exception ignore) {
      // ignore exception and continue
   }
   // do your other stuff here
}

private static void copyFile(File source, File dest)
        throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } 
    finally{
       if (inputChannel != null) inputChannel.close();
       if (outputChannel != null) outputChannel.close();
    }
}

Autres conseils

In your catch block, you can use a continue statement to "skip" the file currently being processed.

Something like below (also incorporates Prabhakaran's suggestion of null checking values):

private static void copyFile(File source, File dest)
        throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } catch (Exception e) {
        // You should be logging any exception here. Empty blocks == bad practice.
        continue;
    } finally {
        if(inputChannel != null {
            inputChannel.close();
        }
        if(outputChannel != null {
            outputChannel.close();
        }
    }
}

change

finally{
       inputChannel.close();
       outputChannel.close();
}

to

finally{
   try {
         if(inputChannel!=null)
            inputChannel.close();
         if(outputChannel!=null)   
            outputChannel.close();         
    } catch (Exception e){

    }
}

and remove the throws IOException from copyFile(File source, File dest) method

now your method look like this

private static void copyFile(File source, File dest){

}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top