Here you can find the source of copyFile(File from, File to)
public static void copyFile(File from, File to) throws IOException
//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("'")); } } }