Here you can find the source of copyFile(File in, File out)
public static void copyFile(File in, File out) throws IOException
//package com.java2s; /*//from w ww.j a v a 2 s .co m * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved. * * The contents of this file are subject to the terms of the GNU General Public License Version 3 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific * language governing permissions and limitations under the License. * * When distributing the software, include this License Header Notice in each file. */ 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 final int CHANNEL_MAX_COUNT = Math.min(64 * 1024 * 1024 - 32 * 1024, Integer.MAX_VALUE); public static void copyFile(File in, File out) throws IOException { copyFile(in, out, CHANNEL_MAX_COUNT); } /** * Copy a file. It is generally not advised to use 0 for <code>maxCount</code> since various * implementations have size limitations, see {@link #copyFile(File, File)}. * * @param in the source file. * @param out the destination file. * @param maxCount the number of bytes to copy at a time, 0 meaning size of <code>in</code>. * @throws IOException if an error occurs. */ public static void copyFile(File in, File out, long maxCount) throws IOException { final FileInputStream sourceIn = new FileInputStream(in); FileOutputStream sourceOut = null; try { sourceOut = new FileOutputStream(out); final FileChannel sourceChannel = sourceIn.getChannel(); final long size = sourceChannel.size(); if (maxCount == 0) maxCount = size; final FileChannel destinationChannel = sourceOut.getChannel(); long position = 0; while (position < size) { position += sourceChannel.transferTo(position, maxCount, destinationChannel); } } finally { sourceIn.close(); if (sourceOut != null) sourceOut.close(); } } public static void copyFile(File in, File out, final boolean useTime) throws IOException { if (!useTime || in.lastModified() != out.lastModified()) { copyFile(in, out); if (useTime) out.setLastModified(in.lastModified()); } } }