Here you can find the source of copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
public static void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
//package com.java2s; //License from project: Open Source License import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { public static void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend) { ensureTargetDirectoryExists(aTargetFile.getParentFile()); FileChannel inChannel = null; FileChannel outChannel = null; FileInputStream inStream = null; FileOutputStream outStream = null; try {/* w ww .j ava 2 s.co m*/ try { inStream = new FileInputStream(aSourceFile); inChannel = inStream.getChannel(); outStream = new FileOutputStream(aTargetFile, aAppend); outChannel = outStream.getChannel(); long bytesTransferred = 0; //defensive loop - there's usually only a single iteration : while (bytesTransferred < inChannel.size()) { bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); } } finally { //being defensive about closing all channels and streams if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); if (inStream != null) inStream.close(); if (outStream != null) outStream.close(); } } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } } public static void ensureTargetDirectoryExists(File aTargetDir) { if (!aTargetDir.exists()) { aTargetDir.mkdirs(); } } }