Here you can find the source of copyFile(File source, File target, boolean replaceIfExists)
private static void copyFile(File source, File target, boolean replaceIfExists) throws IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { private static void copyFile(File source, File target, boolean replaceIfExists) throws IOException { if (!target.createNewFile()) { if (target.exists() && !replaceIfExists) { throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath())); }//w ww .j av a2 s. co m } FileInputStream in = null; FileOutputStream out = null; byte[] b = new byte[8192]; try { in = new FileInputStream(source); out = new FileOutputStream(target); int r; while ((r = in.read(b)) != -1) { out.write(b, 0, r); } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } }