Java BufferedInputStream Copy copyFile(File source, File dest)

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

Description

Copies the file source to dest.
The destination file WILL be overwritten if it's already existed.

License

Open Source License

Parameter

Parameter Description
source source file
dest destination file

Exception

Parameter Description
IOException an exception

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    /**//ww  w .  j  av a2  s. c  o m
     * Copies the file <code>source</code> to <code>dest</code>.</br>
     * The destination file WILL be overwritten if it's already existed.
     * @param source source file
     * @param dest destination file
     * @throws IOException 
     */
    public static void copyFile(File source, File dest) throws IOException {

        if (!dest.exists())
            dest.createNewFile();

        byte[] buffer = new byte[1024];
        int bytesRead = 0;

        try (BufferedInputStream bin = new BufferedInputStream(new FileInputStream(source));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));) {
            while ((bytesRead = bin.read(buffer)) != -1) {
                bos.write(buffer, 0, bytesRead);
            }
        }

    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File inFile, File outFile)
  3. copyFile(File inFile, File outFile)
  4. copyFile(File inputFile, File outputFile)
  5. copyFile(File origem, File destino)
  6. copyFile(File source, File dest, boolean deleteIfExists)
  7. copyFile(File source, File dest, boolean deleteIfExists)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination)