0

I try to use example from this question : How to copy a folder and all its subfolders and files into another folder

I made him static, and when I call copyDirectory(), I have an exception during program runs :

Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
Unhandled exception type IOException

At every line that uses this method.

I added

  throws IOException 

for every method that uses copyDirectory()

Error's count had been shorted, but they remained at native java classes. And I can't edit them : it would be an infinite editing recursion :))

Advance I'm sorry for bad english.

UPD: (Using ApacheCommonsIO)

import org.apache.commons.io.FileUtils;
// the rest import
public class MyClass{
  public myMethod(){
   String src = "/home/user/dir_src";
   String dst = "/home/user/dir_dst";
   FileUtils.copyDirectory(new File(src), new File(dst)); 
  }
}
Community
  • 1
  • 1

1 Answers1

1

2 Things here

  1. You should format your method copyDirectory somehow like this, so it won't throw you all these exceptions:


    public static boolean copyDirectory(File source, File destination) {
      try{
        // Copy Stuff
        return true;
      catch(IOException e){
        // Your way of ErrorLogging
        return false;
      }
    }

  1. For all the IO-Stuff like copying deleting and so on I would recommend you using ApachCommonIO: http://commons.apache.org/io/.

Edit:

Please try this code now, this should at least compile and give you a hint what is wrong:

import org.apache.commons.io.FileUtils;
// the rest import
public class MyClass{
  public myMethod(){
   String src = "/home/user/dir_src";
   String dst = "/home/user/dir_dst";
   try{
      FileUtils.copyDirectory(new File(src), new File(dst)); 
   catch(IOException e){
      e.printStackTrac();
   }
  }
}
Robin
  • 3,512
  • 10
  • 39
  • 73
  • As you recommended, I installed ApachCommonIO. And I have the same problem : my line FileUtils.copyDirectory(new File(src), new File(dst)); is highlighted with "Unhandled IO exception". –  Oct 06 '12 at 19:31
  • Please provide the code you are using. Maybe we will find a fault here. To ApacheCommonsIO, this is a library that provides methods that will handle CommonIO tasks like copy, delete and so on. Please check the Documentation for further information on how to you it. – Robin Oct 07 '12 at 00:59