Here you can find the source of copyFile(File source, File target)
public static void copyFile(File source, File target) throws IOException
//package com.java2s; /* MOD_V2.0//from w ww .ja va 2s. c o m * Copyright (c) 2012 OpenDA Association * All rights reserved. * * This file is part of OpenDA. * * OpenDA is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation, either version 3 of * the License, or (at your option) any later version. * * OpenDA is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with OpenDA. If not, see <http://www.gnu.org/licenses/>. */ import java.io.*; import java.nio.channels.FileChannel; public class Main { /** * Copy file contents from source to destination * using a loop with chunks smaller than 64Mb - 32Kb, * otherwise on Windows, especially when copying to UNC paths, * with files bigger than 64MB, a "java.io.IOException: * Insufficient system resources exist to complete the requested service" could be thrown. * see http://forums.oracle.com/forums/thread.jspa?messageID=8622867 * Also, without using small chunks, on some windows systems * for files larger than about 260 MB could get the error * "java.io.IOException: Map failed". * See http://www.coderanch.com/t/279109/Streams/java/Java-nio */ private static final int MAX_COUNT = (64 * 1024 * 1024) - (32 * 1024); public static void copyFile(File source, File target) throws IOException { FileChannel inChannel = null; FileChannel outChannel = null; try { inChannel = new FileInputStream(source).getChannel(); outChannel = new FileOutputStream(target).getChannel(); //Copy file contents from source to destination //Use a loop with chunks smaller than 64Mb - 32Kb, //otherwise on Windows, especially when copying to UNC paths, //with files bigger than 64MB, a "java.io.IOException: //Insufficient system resources exist to complete the requested service" could be thrown. //see http://forums.oracle.com/forums/thread.jspa?messageID=8622867 //Also, without using small chunks, on some windows systems //for files larger than about 260 MB could get the error //"java.io.IOException: Map failed". //See http://www.coderanch.com/t/279109/Streams/java/Java-nio long fileSize = inChannel.size(); long position = 0; while (position < fileSize) { position += inChannel.transferTo(position, MAX_COUNT, outChannel); } } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } copyFilePermissions(source, target); } /** * Sets the file permissions of the given source file * on the given target file. * * @param source * @param target */ private static void copyFilePermissions(File source, File target) { if (source.canRead()) { target.setReadable(true, false); } else { target.setReadable(false, false); } if (source.canWrite()) { target.setWritable(true, false); } else { target.setWritable(false, false); } if (source.canExecute()) { target.setExecutable(true, false); } else { target.setExecutable(false, false); } } }