Java File Copy copyFiles(String fromDirName, String toDirName)

Here you can find the source of copyFiles(String fromDirName, String toDirName)

Description

Copy all the files under fromDirName to toDirName

License

Apache License

Parameter

Parameter Description
fromDirName a parameter
toDirName a parameter

Declaration

public static boolean copyFiles(String fromDirName, String toDirName) 

Method Source Code

//package com.java2s;
/**************************************************************
 * /*from w  ww . ja v a  2s  .  co m*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 * 
 *************************************************************/

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

import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    /**
     * Copy all the files under fromDirName to toDirName
     * 
     * @param fromDirName
     * @param toDirName
     * @return
     */
    public static boolean copyFiles(String fromDirName, String toDirName) {
        boolean res = true;

        File fromDir = new File(fromDirName);
        if (!fromDir.exists() || !fromDir.isDirectory() || !fromDir.canRead()) {
            System.err.println(fromDir.getAbsolutePath() + "doesn't exist, or isn't file, or can't be read");
            return false;
        }
        File[] files = fromDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory()) {
                res = res && copyDir(fromDirName + "/" + files[i].getName(), toDirName + "/" + files[i].getName());
            } else
                res = res && copyFile(fromDirName + "/" + files[i].getName(), toDirName + "/" + files[i].getName());
        }
        return res;
    }

    /**
     * Recursively copy all files in the source dir into the destination dir
     * 
     * @param fromDirName
     *            the source dir
     * @param toDirName
     *            the destination dir
     * @return
     */
    public static boolean copyDir(String fromDirName, String toDirName) {
        return copyDir(new File(fromDirName), new File(toDirName), true);
    }

    /**
     * Copy all files in the source dir into the destination dir
     * 
     * @param fromDir
     * @param toDir
     * @param recursive
     * @return
     */
    public static boolean copyDir(File fromDir, File toDir, boolean recursive) {
        if (!fromDir.exists() || !fromDir.isDirectory()) {
            System.err.println("The source dir doesn't exist, or isn't dir.");
            return false;
        }
        if (toDir.exists() && !toDir.isDirectory())
            return false;
        boolean result = true;
        toDir.mkdirs();
        File[] files = fromDir.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].isDirectory() && recursive)
                result &= copyDir(files[i], new File(toDir, files[i].getName()), true);
            else
                result &= copyFile(files[i], toDir);
        }

        return result;
    }

    /**
     * Copy a file
     * 
     * @param fromFile
     * @param toFile
     * @return
     */
    public static boolean copyFile(File fromFile, File toFile) {
        if (toFile.isDirectory())
            toFile = new File(toFile, fromFile.getName());

        FileInputStream from = null;
        FileOutputStream to = null;
        try {
            from = new FileInputStream(fromFile);
            File p = toFile.getParentFile();
            if (p != null && !p.exists())
                p.mkdirs();
            to = new FileOutputStream(toFile);
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead);

            return true;
        } catch (IOException e) {
            // Can't copy
            e.printStackTrace();
            return false;
        } finally {
            if (from != null)
                try {
                    from.close();
                } catch (IOException e) {
                }
            if (to != null)
                try {
                    to.close();
                } catch (IOException e) {
                }
        }
    }

    /**
     * Copy a file
     * 
     * @param fromFileName
     * @param toFileName
     * @return
     */
    public static boolean copyFile(String fromFileName, String toFileName) {
        return copyFile(new File(fromFileName), new File(toFileName));
    }
}

Related

  1. copyFiles(File src, File dest, String... filenameExtension)
  2. copyFiles(File srcDir, File destDir, String... regexps)
  3. copyFiles(File srcFile, File destFolder)
  4. copyFiles(InputStream inStream, FileOutputStream outStream)
  5. copyFiles(String from, String to)
  6. copyFileToPath(String fileToPath, String fileName, String sourceFile)
  7. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  8. doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
  9. fileCopy(File source, String dest)