发布于

java复制文件夹


public static void copyDirectiory(String sourceDir, String targetDir) {
    if (!(new File(sourceDir)).isDirectory())
      return; 
    (new File(targetDir)).mkdirs();
    
    File[] file = (new File(sourceDir)).listFiles();
    
    for (int i = 0; i < file.length; i++) {
      if (file[i].isFile()) {
        
        File sourceFile = file[i];

        
        File targetFile = new File((new File(targetDir)).getAbsolutePath() + File.separator + file[i].getName());
        copyFile(sourceFile, targetFile);
      } 
      
      if (file[i].isDirectory()) {
        
        String dir1 = sourceDir + File.separator + file[i].getName();
        
        String dir2 = targetDir + File.separator + file[i].getName();
        
        copyDirectiory(dir1, dir2);
      } 
    } 
评论