Java BufferedInputStream Copy copyFile(File src, File target)

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/*//from   w  w  w .j  a  v  a2  s  .  c o  m
 * Copyright (C) ${year} Omry Yadan <${email}>
 * All rights reserved.
 *
 * See https://github.com/omry/banana/blob/master/BSD-LICENSE for licensing information
 */

import java.io.*;

public class Main {
    public static void copyFile(File src, File target) throws IOException {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(target));
            pump(in, out);
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                }
            if (out != null)
                try {
                    out.close();
                } catch (IOException e) {
                }
        }
    }

    /**
     * Writes the bytes read from the given input stream into the given output
     * stream until the end of the input stream is reached. Returns the amount of
     * bytes actually read/written.
     */
    public static int pump(InputStream in, OutputStream out) throws IOException {
        byte[] buf = new byte[4096];
        int count;
        int amountRead = 0;

        while ((count = in.read(buf)) != -1) {
            out.write(buf, 0, count);
            amountRead += count;
        }

        return amountRead;
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File targetDir, boolean onlyNew)
  7. copyFile(File src, String target)
  8. copyFile(File srcFile, File destFile, boolean createCopy)
  9. copyFile(File srcFile, File detFolder)