Java Copy File copyFiles(String srcPath, String destPath)

Here you can find the source of copyFiles(String srcPath, String destPath)

Description

copy Files

License

Open Source License

Declaration

public static void copyFiles(String srcPath, String destPath) throws IOException 

Method Source Code


//package com.java2s;
/*/* w w w  .  j a  v  a 2s .  c om*/
Copyright 2007-2009 QSpin - www.qspin.be
    
This file is part of QTaste framework.
    
QTaste is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
QTaste is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with QTaste. If not, see <http://www.gnu.org/licenses/>.
*/

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

public class Main {
    public static void copyFiles(String srcPath, String destPath) throws IOException {
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        destFile.mkdirs();
        if (srcFile.isDirectory() && destFile.isDirectory())
            copyFiles(srcFile, destFile);
    }

    public static void copyFiles(File src, File dest) throws IOException {
        if (src.isDirectory()) {
            dest.mkdirs();
            String list[] = src.list();
            for (int i = 0; i < list.length; i++) {
                String dest1 = dest.getPath() + "/" + list[i];
                String src1 = src.getPath() + "/" + list[i];
                // i.e: avoid .svn directory
                File src1_ = new File(src1);
                File dest1_ = new File(dest1);
                if (!src1_.isHidden())
                    copyFiles(src1_, dest1_);
            }
        } else {

            FileInputStream fin = new FileInputStream(src);
            FileOutputStream fout = new FileOutputStream(dest);
            int c;
            while ((c = fin.read()) >= 0) {
                fout.write(c);
            }
            fin.close();
            fout.close();
        }
    }
}

Related

  1. copyFiles(String p_source, String p_target)
  2. copyFiles(String srcFilePath, String destFilePath)
  3. copyFiles(String srcFolderStr, String destFolderStr)
  4. CopyFiles(String strOrgFile, String strDestFile, boolean bFailIfExists)
  5. copyFiles(String strPath, String dstPath, String srcExtension)
  6. copyFiles(String[] sourceFiles, String toDir, boolean overwrite)
  7. copyFileSafe(File destinationFileName, File sourceFileName)