我写了个application的,把c:\的文件拷到d:\temp里,你看看
import java.io.*;public class Application3 {
  boolean packFrame = false;  public Application3() {
    copyDir(new File("c:/"),new File("d:/temp/"));
    System.out.println(new File("d:/temp/").getPath()+File.separator);
  }  public void copyDir(File src_path,File dest_path){
    File[] files = src_path.listFiles();
    for(int i=0;i<files.length;i++){
      File src=files[i];
      File dest=new File(dest_path.getPath()+File.separator+files[i].getName());
      if(!src.isDirectory())
        copyFile(src,dest);
    }
  }  public void copyFile(File src,File dest){
    try{
      System.out.println(src.getAbsoluteFile()+" -> "+dest.getAbsoluteFile());
      FileInputStream in = new FileInputStream(src);
      FileOutputStream out = new FileOutputStream(dest);
      byte[] buffer = new byte[1024];
      int length = -1;
      while((length=in.read(buffer))!=-1){
              out.write(buffer,0,length);
      }
      out.flush();
      out.close();
      in.close();
      System.out.println("文件复制成功!");
    }catch(Exception e){
      System.out.println("文件复制失败!");
    }
  }
  
  public static void main(String[] args) {
    new Application3();
  }
}

解决方案 »

  1.   

    我试过你的程序了,但是好像你的程序和我的要求不太一样,我要将c:\所有的文件和文件夹都要拷贝到d:\temp里,就是里面的所有的内容都要拷贝,但是你的程序好像只能拷贝文件,不能拷贝文件夹哦!!
      

  2.   

    稍微改一下就可以了,完整代码如下:
    import java.io.*;public class Application3 {
      boolean packFrame = false;  public Application3() {
        copyDir(new File("c:/temp/"),new File("d:/temp/"));
      }  public void copyDir(File src_path,File dest_path){
        if(!dest_path.exists())
          dest_path.mkdirs();
        File[] files = src_path.listFiles();
        for(int i=0;i<files.length;i++){
          File src=files[i];
          File dest=new File(dest_path.getPath()+File.separator+files[i].getName());
          if(!src.isDirectory())
            copyFile(src,dest);
          else
            copyDir(src,dest);
        }
      }  public void copyFile(File src,File dest){
        try{
          System.out.println(src.getAbsoluteFile()+" -> "+dest.getAbsoluteFile());
          FileInputStream in = new FileInputStream(src);
          FileOutputStream out = new FileOutputStream(dest);
          byte[] buffer = new byte[1024];
          int length = -1;
          while((length=in.read(buffer))!=-1){
                  out.write(buffer,0,length);
          }
          out.flush();
          out.close();
          in.close();
          System.out.println("文件复制成功!");
        }catch(Exception e){
          System.out.println("文件复制失败!");
        }
      }  public static void main(String[] args) {
        new Application3();
      }
    }