Java File Copy copyFiles(File file, File destPath)

Here you can find the source of copyFiles(File file, File destPath)

Description

Copy files to destPath, can be a folder.

License

Apache License

Parameter

Parameter Description
file a parameter
destPath a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFiles(File file, File destPath) throws IOException 

Method Source Code


//package com.java2s;
/*/*from w ww .jav  a 2s .  c o m*/
 * Copyright (C) 2013 The Open Source Project By Yunying.Zhang
 *
 * Licensed 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.
 * author:Yunying.Zhang
 * date:2013-09-27
 */

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

public class Main {
    public static final int BUFFER_SIZE = 1024 * 1024 * 1;

    /**
     * Copy files to destPath, can be a folder.
     * @param file
     * @param destPath
     * @throws IOException
     */
    public static void copyFiles(File file, File destPath) throws IOException {
        if (file.isDirectory()) {
            File dest = new File(destPath, file.getName());
            File[] files = file.listFiles();
            for (File subFile : files) {
                copyFiles(subFile, dest);
            }
        } else {
            copyFile(file, destPath);
        }
    }

    /**
     * Copy a file to destPath. only file.
     * @param file
     * @param destPath
     * @throws IOException
     */
    public static void copyFile(File file, File destPath) throws IOException {
        if (file.isDirectory()) {
            return;
        }
        if (!destPath.exists()) {
            destPath.mkdirs();
        }
        FileInputStream fis = new FileInputStream(file);
        FileOutputStream fos = new FileOutputStream(new File(destPath, file.getName()));
        byte[] buf = new byte[BUFFER_SIZE];
        int length = -1;
        while ((length = fis.read(buf)) != -1) {
            fos.write(buf, 0, length);
        }
        fos.flush();
        fos.close();
        fis.close();
    }
}

Related

  1. copy(File from, File to)
  2. copyFile(File source, File target)
  3. copyFile(File src, File dest, Component parentComponent, String message)
  4. copyFileFromFileSystem(String sourceFileName, File target)
  5. copyFiles(File fromDir, File toDir)
  6. copyFiles(File source, File destination)
  7. copyFiles(File sourceFile, File targetDir)
  8. copyFiles(File src, File dest)