Here you can find the source of copyFile(File copyFrom, File copyTo)
public static boolean copyFile(File copyFrom, File copyTo) throws IOException
//package com.java2s; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; 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; import java.nio.channels.FileLock; public class Main { public static boolean copyFile(File copyFrom, File copyTo) throws IOException { if (!isLockingSupported()) { return copyFileNormal(copyFrom, copyTo); } else {/*w w w.ja v a 2s. co m*/ return copyFileLocking(copyFrom, copyTo); } } public static boolean isLockingSupported() { boolean bool1 = System.getProperty("java.specification.version").compareTo("1.4") >= 0; boolean bool2 = (!(System.getProperty("os.name").toLowerCase().equals("hp-ux"))) || (!(System.getProperty("os.arch").toLowerCase().startsWith("ia64"))); return bool1 && bool2; } public static boolean copyFileNormal(File copyFrom, File copyTo) throws IOException { if (!copyFrom.exists() || copyTo == null) { return false; } byte[] arrayOfByte = new byte[4096]; BufferedInputStream input = new BufferedInputStream(new FileInputStream(copyFrom)); BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(copyTo, false)); int i; while ((i = input.read(arrayOfByte, 0, arrayOfByte.length)) != -1) { output.write(arrayOfByte, 0, i); } output.close(); input.close(); return true; } public static boolean copyFileLocking(File copyFrom, File copyTo) throws FileNotFoundException, IOException { if ((!(copyFrom.exists())) || (copyTo == null)) { return false; } byte[] arrayOfByte = new byte[4096]; FileOutputStream fileOutput = new FileOutputStream(copyTo, true); try { FileChannel channel = fileOutput.getChannel(); FileLock fileLock = channel.tryLock(); if (fileLock != null) { channel.truncate(0L); BufferedInputStream input = new BufferedInputStream(new FileInputStream(copyFrom)); BufferedOutputStream output = new BufferedOutputStream(fileOutput); try { int i; while ((i = input.read(arrayOfByte, 0, arrayOfByte.length)) != -1) { output.write(arrayOfByte, 0, i); } output.flush(); fileLock.release(); } catch (Exception ex) { throw new RuntimeException("copyFileLocking ERR : " + ex.getMessage(), ex); } finally { output.close(); input.close(); } return true; } } catch (Exception ex) { throw new RuntimeException("copyFileLocking ERR : " + ex.getMessage(), ex); } finally { fileOutput.close(); } throw new IOException("Unable to acquire file lock for " + copyTo); } }