Java Directory Copy copyDirectory(File file, File folderDestino)

Here you can find the source of copyDirectory(File file, File folderDestino)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File file, File folderDestino) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static void copyDirectory(File file, File folderDestino) throws IOException {
        if (file != null && file.exists()) {
            File newFile = new File(folderDestino.getAbsolutePath() + File.separator + file.getName());
            if (file.isDirectory()) {
                newFile.mkdir();// w w  w  .j  a va 2  s . c  o m
                for (File f : file.listFiles()) {
                    copyDirectory(f, newFile);
                }
            } else {
                copyFile(file, newFile);
            }
        }
    }

    public static void copyFile(File orig, File dest) throws IOException {
        FileInputStream fis = new FileInputStream(orig);
        FileOutputStream fos = new FileOutputStream(dest);
        copyData(fis, fos);

    }

    public static void copyData(InputStream fis, OutputStream fos) throws IOException {
        try {
            byte[] buff = new byte[50000];
            int readed = -1;
            while ((readed = fis.read(buff)) > 0) {
                fos.write(buff, 0, readed);
            }
        } finally {
            fis.close();
            fos.close();
        }
    }
}

Related

  1. copyDirectory(File from, File to)
  2. copyDirectory(File inDir, File outDir)
  3. copyDirectory(File inDir, File outDir)
  4. copyDirectory(File inputFolder, File outputFolder)