Here you can find the source of copyFile(File from, File to)
public static void copyFile(File from, File to) throws IOException
//package com.java2s; /**/*from ww w. java 2 s.c o m*/ * Copyright (C) 2009-2012 BonitaSoft S.A. * BonitaSoft, 32 rue Gustave Eiffel - 38000 Grenoble * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 2.0 of the License, or * (at your option) any later version. * * This program 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 General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. */ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { public static int bufferSize = 2 * 8192; public static void copyFile(File from, File to) throws IOException { if (from.isDirectory()) { copyDir(from, to); } else { FileInputStream fromIn = new FileInputStream(from); FileOutputStream toOut = new FileOutputStream(to); try { copy(fromIn, toOut); } finally { fromIn.close(); toOut.close(); } } } public static void copyDir(File from, File to) throws IOException { if (to.exists()) { if (!to.isDirectory()) { throw new IllegalArgumentException(to.toString()); } } else { to.mkdirs(); } File[] files = from.listFiles(new FilenameFilter() { @Override public boolean accept(File arg0, String arg1) { return !".svn".equals(arg1); } }); if (files != null) { for (int i = 0; i < files.length; i++) { String name = files[i].getName(); if (".".equals(name) || "..".equals(name)) { continue; } copy(files[i], new File(to, name)); } } } public static void copy(File from, File to) throws IOException { if (from.isDirectory()) { copyDir(from, to); } else { FileInputStream fromIn = new FileInputStream(from); FileOutputStream toOut = new FileOutputStream(to); try { copy(fromIn, toOut); } finally { fromIn.close(); toOut.close(); } } } public static void copy(InputStream in, OutputStream out) throws IOException { copy(in, out, -1); } public static void copy(InputStream in, OutputStream out, long byteCount) throws IOException { byte buffer[] = new byte[bufferSize]; int len = bufferSize; if (byteCount >= 0) { while (byteCount > 0) { int max = byteCount < bufferSize ? (int) byteCount : bufferSize; len = in.read(buffer, 0, max); if (len == -1) { break; } byteCount -= len; out.write(buffer, 0, len); } } else { while (true) { len = in.read(buffer, 0, bufferSize); if (len < 0) { break; } out.write(buffer, 0, len); } } out.close(); in.close(); } }