Java Copy Directory copyDirectory(String srcDir, String destDir)

Here you can find the source of copyDirectory(String srcDir, String destDir)

Description

copy Directory

License

Apache License

Declaration

public static void copyDirectory(String srcDir, String destDir) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class Main {
    public static void copyDirectory(String srcDir, String destDir) {
        try {/* ww  w  .  jav a 2s.  c  o m*/
            File dDir = new File(destDir);
            File sDir = new File(srcDir);
            //        if(!new File(srcDir).exists() && new File(destDir).exists())
            //        return ;
            dDir.mkdirs();
            File[] file = sDir.listFiles();
            int flength = file.length;
            for (int i = 0; i < flength; i++) {
                if (file[i].isFile() && !file[i].getName().endsWith(".dll")) {
                    File sourceFile = file[i];
                    File targetFile = new File(dDir.getAbsolutePath()
                            + File.separator + file[i].getName());
                    CopyFile(sourceFile, targetFile);
                }
                if (file[i].isDirectory()
                        && file[i].getName().equals("granulej")) {
                    String dir1 = srcDir + "/" + file[i].getName();
                    String dir2 = destDir + "/" + file[i].getName();
                    copyDirectory(dir1, dir2);
                }
                if (file[i].isDirectory()
                        && file[i].getName().equals("lang")) {
                    String dir1 = srcDir + "/" + file[i].getName();
                    String dir2 = destDir + "/" + file[i].getName();
                    copyDirectory(dir1, dir2);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void CopyFile(File srcFile, File destFile) {
        try {
            int bytesum = 0;
            int byteread = 0;
            InputStream inStream = new FileInputStream(srcFile);
            FileOutputStream fs = new FileOutputStream(destFile);
            byte[] buffer = new byte[1444];
            while ((byteread = inStream.read(buffer)) != -1) {
                bytesum += byteread;
                fs.write(buffer, 0, byteread);
            }
            inStream.close();
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void CopyFile(String srcFile, String destFile) {
        try {
            File src = new File(srcFile);
            File dest = new File(destFile);
            if (!dest.exists()) {
                dest.createNewFile();
                CopyFile(src, dest);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyDirectory(File srcPath, File dstPath)
  2. copyDirectory(final File sourceDir, final File destDir)
  3. copyDirectory(final File src, final File dest)
  4. copyDirectory(final String sourceDir, final String targetDir)
  5. copyDirectory(String sourceDir, String targetDir)
  6. copyDirectoryAndOverwriteFilesIfNeeded(File srcDir, File destDir)
  7. copyDirectoryContent(File srcPath, File targetPath)
  8. copyDirectoryContents(File directory, String targetDirName, boolean update)
  9. copyDirectoryContents(File source, File destination)