Java BufferedInputStream Copy copyFile(File source, File target)

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

Description

Binary copy of a file from source to target.

License

Apache License

Declaration

public static void copyFile(File source, File target)
        throws IOException 

Method Source Code

//package com.java2s;
/* Copyright (C) 2003-2016 Patrick G. Durand
 *
 * 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.//  w  w w .j  av a  2  s  .  c om
 */

import java.io.BufferedInputStream;

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

import java.io.IOException;

public class Main {
    /**
     * Binary copy of a file from source to target.
     */
    public static void copyFile(File source, File target)
            throws IOException {
        FileOutputStream fos = null;
        FileInputStream fis = null;
        BufferedInputStream bis;
        int n;
        byte[] buf = new byte[2048];

        try {
            fos = new FileOutputStream(target);
            fis = new FileInputStream(source);
            bis = new BufferedInputStream(fis);
            while ((n = bis.read(buf)) != -1) {
                fos.write(buf, 0, n);
            }
            fos.flush();
        } catch (IOException e) {
            throw e;
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (Exception ex) {
            }
            try {
                if (fis != null)
                    fis.close();
            } catch (Exception ex) {
            }
        }
    }
}

Related

  1. copyFile(File source, File destination)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination, boolean overwrite)
  4. copyFile(File source, File target)
  5. copyFile(File source, File target)
  6. copyFile(File sourceFile, File targetFile)
  7. copyFile(File sourceFile, File targetFile)
  8. copyFile(File sourceFile, File targetFile)
  9. copyFile(File src, File dest)