Here you can find the source of copyFile(String fromFileName, String toFileName)
public static boolean copyFile(String fromFileName, String toFileName) throws IOException
//package com.java2s; /*// w w w . j a v a 2s. c o m * (C) Copyright IBM Corp. 2008 * * LICENSE: Eclipse Public License v1.0 * http://www.eclipse.org/legal/epl-v10.html */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { public static boolean copyFile(String fromFileName, String toFileName) throws IOException { return copyFile(new File(fromFileName), new File(toFileName)); } public static boolean copyFile(File fromFile, File toFile) throws IOException { // System.out.println("Testing if files exist. " // + "fromFile: " + fromFile.getCanonicalPath() + ": " + fromFile.exists() // + "; toFile: " + toFile.getCanonicalPath() + ": " + toFile.exists() // ); if (!fromFile.exists()) return false; if (!toFile.exists()) toFile.createNewFile(); FileChannel fromChannel = null, toChannel = null; try { fromChannel = new FileInputStream(fromFile).getChannel(); toChannel = new FileOutputStream(toFile).getChannel(); toChannel.transferFrom(fromChannel, 0, fromChannel.size()); return true; } finally { if (null != fromChannel) fromChannel.close(); if (null != toChannel) toChannel.close(); } } }