Java Directory Copy copyDirectory(File source, File target)

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

Description

copy the dirictory(include sub folders and files) from the source to the target, the copyed dirictory will be under the target.

License

Open Source License

Parameter

Parameter Description
source a parameter
target a parameter

Declaration

public static void copyDirectory(File source, File target) 

Method Source Code

//package com.java2s;
// %InstallDIR%\features\org.talend.rcp.branding.%PRODUCTNAME%\%PRODUCTNAME%license.txt

import java.io.File;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;

import java.io.OutputStream;

public class Main {
    /**/*  w ww .  j  ava  2 s .  c o m*/
     * copy the dirictory(include sub folders and files) from the source to the target, the copyed dirictory will be
     * under the target.
     * 
     * @param source
     * @param target
     */
    public static void copyDirectory(File source, File target) {
        File tarpath = new File(target, source.getName());
        if (source.isDirectory()) {
            tarpath.mkdir();
            File[] dir = source.listFiles();
            for (File element : dir) {
                copyDirectory(element, tarpath);
            }
        } else {
            try {
                InputStream is = new FileInputStream(source);
                OutputStream os = new FileOutputStream(tarpath);
                byte[] buf = new byte[1024];
                int len = 0;
                while ((len = is.read(buf)) != -1) {
                    os.write(buf, 0, len);
                }
                is.close();
                os.close();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyDirectory(File source, File destination, boolean overwrite)
  2. copyDirectory(File source, File destination, String endWith)
  3. copyDirectory(File source, File target)
  4. copyDirectory(File source, File target)
  5. copyDirectory(File source, File target)
  6. copyDirectory(File source, File target)
  7. copyDirectory(File source, File target)
  8. copyDirectory(File sourceDir, File destDir)
  9. copyDirectory(File sourceDir, File destDir)