Java Copy File copyFiles(String srcFolderStr, String destFolderStr)

Here you can find the source of copyFiles(String srcFolderStr, String destFolderStr)

Description

copy Files

License

BSD License

Declaration

public static void copyFiles(String srcFolderStr, String destFolderStr) throws IOException 

Method Source Code

//package com.java2s;
/*L//from   w w w  . j a va2s  .co  m
 *  Copyright Ekagra Software Technologies Ltd.
 *  Copyright SAIC, SAIC-Frederick
 *
 *  Distributed under the OSI-approved BSD 3-Clause License.
 *  See http://ncip.github.com/cacore-sdk/LICENSE.txt for details.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

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

import java.util.List;

public class Main {
    public static void copyFiles(String srcFolderStr, String destFolderStr) throws IOException {
        copyFiles(srcFolderStr, destFolderStr, null);
    }

    public static void copyFiles(String srcFolderStr, String destFolderStr, List exclude) throws IOException {
        File srcFolder = new File(srcFolderStr);
        File destFolder = new File(destFolderStr);

        if (srcFolder.isDirectory()) {

            // if directory not exists, create it
            if (!destFolder.exists()) {
                destFolder.mkdir();
                //System.out.println("Directory copied from " + srcFolder + "  to " + destFolder);
            }

            String files[] = srcFolder.list();

            for (String file : files) {
                File srcFile = new File(srcFolder, file);
                if (exclude != null && exclude.contains(srcFile.getName()))
                    continue;
                File destFile = new File(destFolder, file);
                copyFile(srcFile, destFile);
            }

        }
    }

    public static void copyFile(File src, File dest) throws IOException {
        // if file, then copy it
        // Use bytes stream to support all file types
        InputStream in = new FileInputStream(src);
        OutputStream out = new FileOutputStream(dest);

        byte[] buffer = new byte[1024];

        int length;
        // copy the file content in bytes
        while ((length = in.read(buffer)) > 0) {
            out.write(buffer, 0, length);
        }

        in.close();
        out.close();
        //System.out.println("File copied from " + src + " to " + dest);

    }
}

Related

  1. copyFiles(String p_source, String p_target)
  2. copyFiles(String srcFilePath, String destFilePath)
  3. copyFiles(String srcPath, String destPath)
  4. CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)
  5. copyFiles(String strPath, String dstPath, String srcExtension)
  6. copyFiles(String[] sourceFiles, String toDir, boolean overwrite)