Java BufferedInputStream Copy copyFile(File from, File to)

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

Description

Copy a file from one location to the next.

License

LGPL

Parameter

Parameter Description
from from
to target

Exception

Parameter Description
IOException on error

Declaration

public static void copyFile(File from, File to) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

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

import java.io.OutputStream;

public class Main {
    /**/*from  w ww .  j  av a  2s.  co m*/
     * Copy a file from one location to the next.
     * 
     * @param from from
     * @param to target
     * @throws IOException on error
     */
    public static void copyFile(File from, File to) throws IOException {
        InputStream in = null;
        OutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(from));
            out = new BufferedOutputStream(new FileOutputStream(to));
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            close(in);
            close(out);
        }
    }

    /**
     * Closes a {@link Closeable} object, eating {@link IOException} errors.
     * Will do nothing if the passed object is null.
     * 
     * @param o {@link Closeable} object
     */
    public static void close(Closeable o) {
        if (o == null)
            return;

        try {
            o.close();
        } catch (IOException e) {
        }
    }
}

Related

  1. copyFile(File file, File ziel)
  2. copyFile(File from, File to)
  3. copyFile(File from, File to)
  4. copyFile(File from, File to)
  5. copyFile(File fromFile, File toFile)
  6. copyFile(File fSrcFile, File fDestFile)