Java如何实现把一个目录里面的所有东西复制到另一个目录里面,
例如
我想把C:\java这个文件夹复制到D:\下,用程序怎么实现??

解决方案 »

  1.   

    import java.io.* ;//method
    public void copyTo(String srcDir,String desDir){
       File ds = new File(srcDir) ;
       ds.renameTo(desDir) ;
    }
      

  2.   

    你也可以在java中调用xcopy命令
      

  3.   

    不是用DOS命令,用其他的办法可以吗
      

  4.   

    拷贝目录,下例只可以拷贝目录中的文件:/**
    * This class copies an input files of a directory to another directory not include subdir
    *
    * @param String sourcedir the directory to copy from such as:/home/bqlr/images
    * @param String destdir the target directory
    */ 
    public static void CopyDir(String sourcedir,String destdir) throws Exception
    {
      File dest = new File(destdir);
      File source = new File(sourcedir);  String [] files= source.list();
      try
      {
        destdir.mkdirs();
      }catch (Exception ex) {
        throw new Exception("CopyDir:"+ex.getMessage()); 
      }
      for (int i = 0; i < files.length; i++) 
      { 
        String sourcefile = source+File.separator+files[i];
        String destfile = dest+File.separator+files[i];
        File temp = new File(sourcefile);
        if (temp.isFile()){
          try{
            copy(sourcefile,destfile);
          }catch (Exception ex) {
            throw new Exception("CopyDir:"+ex.getMessage()); 
          }
        }
      }
    }
    拷贝文件,相当于linux中cp命令,但与平台无关,可以拷贝文本 或二进制文件:/**
    * This class copies an input file to output file
    *
    * @param String input file to copy from
    * @param String output file
    */
    public static boolean copy(String input, String output) throws Exception{
      int BUFSIZE = 65536;
      try{
        FileInputStream fis = new FileInputStream(input);
        FileOutputStream fos = new FileOutputStream(output);    int s;
        byte[] buf = new byte[BUFSIZE];
        while ((s = fis.read(buf)) > -1 ){
          fos.write(buf, 0, s); 
        }  }catch (Exception ex) {
        throw new Exception("makehome"+ex.getMessage()); 
      }
      return true;
    }