Here you can find the source of copyFile(final File source, final File destination, final boolean overwrite)
public static void copyFile(final File source, final File destination, final boolean overwrite) 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; import java.io.InputStream; import java.io.OutputStream; public class Main { public static void copyFile(final File source, final File destination, final boolean overwrite) throws IOException { if (source == null) { throw new NullPointerException("source"); }/* ww w .j a v a2 s.c o m*/ if (!source.exists()) { throw new IllegalArgumentException("source"); } if (!source.canRead()) { throw new IllegalArgumentException("source"); } if (!source.isFile()) { throw new IllegalArgumentException("source"); } if (destination == null) { throw new NullPointerException("destination"); } if (destination.exists() && !destination.isFile()) { throw new IllegalArgumentException("destination"); } if (destination.exists() && !destination.canWrite()) { throw new IllegalArgumentException("destination"); } File destinationParent = destination.getParentFile(); destinationParent.mkdirs(); if (!destinationParent.exists()) { throw new IllegalArgumentException("destinationParent"); } if (!destinationParent.canWrite()) { throw new IllegalArgumentException("destinationParent"); } if (!destinationParent.isDirectory()) { throw new IllegalArgumentException("destinationParent"); } if (destination.exists() && !overwrite) { return; } InputStream is = null; BufferedInputStream bis = null; OutputStream os = null; BufferedOutputStream bos = null; try { is = new FileInputStream(source); bis = new BufferedInputStream(is); os = new FileOutputStream(destination); bos = new BufferedOutputStream(os); byte[] buffer = new byte[2048]; int length = 0; while ((length = bis.read(buffer)) > 0) { bos.write(buffer, 0, length); } } finally { try { try { if (is != null) { is.close(); } } finally { if (bis != null) { bis.close(); } } } finally { try { if (bos != null) { bos.close(); } } finally { if (os != null) { os.close(); } } } } } }