How to continue copying on error with copyDirectory from FileUtils (Apache Commons IO 2.4)

Posted by Exocom on Stack Overflow See other posts from Stack Overflow or by Exocom
Published on 2012-12-18T10:53:39Z Indexed on 2012/12/18 11:03 UTC
Read the original article Hit count: 137

Filed under:
|
|

I am trying to copy files from one folder to another using the FileUtils method "static void copyDirectory(File srcDir, File destDir)" from apache commons io (2.4) with the following code:

String srcDir  = "/sourceDirectory/examples/";
String destDir = "/tmp/examples/";
try{
    FileUtils.copyDirectory(new File(srcDir), new File(destDir));
} catch (IOException e){
    e.printStackTrace();
}

The file structure looks like the following:

examples/                           (read access)
examples/.subdirectory              (NO access)
examples/file1.txt                  (read access)
examples/file2.txt                  (read access)
examples/subdirectory2/file1.txt    (read access)
examples/subdirectory2/file2.txt    (NO access)

The problem is, that in srcDir there are a few files and one subdirectory I don't have access to. FileUtils.copyDirectory throws an "java.io.FileNotFoundException: .subdirectory (Permission denied)" and aborts.

Is it possible to ignore those files I don't have permission to and just copy all the other files I can read?

© Stack Overflow or respective owner

Related posts about java

Related posts about apache-commons-io