Java FileInputStream Copy copyFile(File file, File destPath)

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

Description

Copy a file to destPath.

License

Apache License

Parameter

Parameter Description
file a parameter
destPath a parameter

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code


//package com.java2s;
/*/*from w w  w  .j ava2 s  .c om*/
 * 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 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. copyFile(File copyFrom, File copyTo)
  2. copyFile(File destfile, File srcfile)
  3. copyFile(File destFile, File srcFile)
  4. copyFile(File destination, File source)
  5. copyFile(File existingFile, File destFile)
  6. copyFile(File file, File dir)
  7. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  8. copyFile(File file, OutputStream os)
  9. copyFile(File fileSrc, File fileDest)