Java BufferedInputStream Copy copyFile(File from, File to)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File from, File to) 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 {
    public static void copyFile(File from, File to) throws IOException {
        createFileSafely(to);//from   w  ww.j av a 2  s  .  c o m
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(from));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(to));
        byte[] block;
        while (bis.available() > 0) {
            block = new byte[16384];
            final int readNow = bis.read(block);
            bos.write(block, 0, readNow);
        }
        bos.flush();
        bos.close();
        bis.close();
    }

    public static void createFileSafely(File file) throws IOException {
        File parentFile = new File(file.getParent());
        if (!parentFile.exists()) {
            if (!parentFile.mkdirs()) {
                throw new IOException("Unable to create parent file: " + file.getParent());
            }
        }
        if (file.exists()) {
            if (!file.delete()) {
                throw new IOException("Couldn't delete '".concat(file.getAbsolutePath()).concat("'"));
            }
        }
        if (!file.createNewFile()) {
            throw new IOException("Couldn't create '".concat(file.getAbsolutePath()).concat("'"));
        }
    }
}

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)
  7. copyFile(File in, File out)