Java tutorial
//package com.java2s; //License from project: Apache License import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /** * Effective way to copy files by file channel. * @return Return the number of copied files. */ public static int fileChannelCopy(File[] sources, File[] targets) { int result = 0; if (sources == null || targets == null) { return result; } FileInputStream fis = null; FileOutputStream fos = null; FileChannel fc_in = null; FileChannel fc_out = null; try { for (int i = 0, len_s = sources.length, len_t = targets.length; i < len_s && i < len_t; ++i) { if (sources[i] == null || targets[i] == null) { continue; } fis = new FileInputStream(sources[i]); fos = new FileOutputStream(targets[i]); fc_in = fis.getChannel(); fc_out = fos.getChannel(); fc_in.transferTo(0, fc_in.size(), fc_out); ++result; } } catch (IOException e) { e.printStackTrace(); } finally { if (fc_out != null) { try { fc_out.close(); } catch (IOException e) { } } if (fos != null) { try { fos.close(); } catch (IOException e) { } } if (fc_in != null) { try { fc_in.close(); } catch (IOException e) { } } if (fis != null) { try { fis.close(); } catch (IOException e) { } } } return result; } public static int fileChannelCopy(File source, File target) { return fileChannelCopy(new File[] { source }, new File[] { target }); } }