Here you can find the source of copyFile(final String sourceFilePath, final String destFilePath)
public static void copyFile(final String sourceFilePath, final String destFilePath) throws IOException
//package com.java2s; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; public class Main { public static void copyFile(final String sourceFilePath, final String destFilePath) throws IOException { final File sourceFile = new File(sourceFilePath); final File destFile = new File(destFilePath); if (!destFile.exists()) { destFile.createNewFile();/* w ww . j ava 2 s . c o m*/ } else { destFile.delete(); destFile.createNewFile(); } FileInputStream source = null; FileOutputStream destination = null; try { source = new FileInputStream(sourceFile); destination = new FileOutputStream(destFile); // previous code: destination.transferFrom(source, 0, // source.size()); // to avoid infinite loops, should be: long count = 0; final long size = source.getChannel().size(); while ((count += destination.getChannel().transferFrom(source.getChannel(), count, size - count)) < size) { ; } } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } } }