Here you can find the source of nioCopyFile(File source, File target, boolean replaceIfExists)
private static void nioCopyFile(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; import java.nio.channels.FileChannel; public class Main { private static void nioCopyFile(File source, File target, boolean replaceIfExists) throws IOException { if (!target.createNewFile()) // atomic {//ww w . ja va 2s. com if (target.exists() && !replaceIfExists) { throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath())); } } FileInputStream sourceStream = null; FileOutputStream targetStream = null; FileChannel sourceChannel = null; FileChannel targetChannel = null; try { sourceStream = new FileInputStream(source); targetStream = new FileOutputStream(target); sourceChannel = sourceStream.getChannel(); targetChannel = targetStream.getChannel(); final long size = sourceChannel.size(); long transferred = 0L; while (transferred < size) { transferred += targetChannel.transferFrom(sourceChannel, transferred, (size - transferred)); } } finally { if (sourceChannel != null) { sourceChannel.close(); } if (targetChannel != null) { targetChannel.close(); } if (sourceStream != null) { sourceStream.close(); } if (targetStream != null) { targetStream.close(); } } } }