0

我尝试使用此问题中的示例:如何将文件夹及其所有子文件夹和文件复制到另一个文件夹中

我让他成为静态的,当我调用 copyDirectory() 时,我在程序运行期间出现异常:

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

在使用此方法的每一行。

我添加了

  throws IOException 

对于每个使用 copyDirectory() 的方法

Error 的计数已被缩短,但它们仍保留在本地 java 类中。而且我无法编辑它们:这将是一个无限的编辑递归:))

提前我很抱歉英语不好。

UPD:(使用 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)); 
  }
}
4

1 回答 1

1

2 这里的东西

  1. 您应该像这样格式化您的方法 copyDirectory ,这样它就不会抛出所有这些异常:


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

  1. 对于所有 IO-Stuff,例如复制删除等,我建议您使用 ApachCommonIO:http ://commons.apache.org/io/ 。

编辑:

请立即尝试此代码,这至少应该可以编译并提示您出了什么问题:

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();
   }
  }
}
于 2012-10-06T18:01:07.613 回答