Here you can find the source of copyFile(File source, File target)
private static void copyFile(File source, File target) throws IOException
//package com.java2s; /******************************************************************************* * Copyright (c) 2013 Red Hat, Inc./*from w ww.j a v a2 s . c om*/ * Distributed under license by Red Hat, Inc. All rights reserved. * This program is made available under the terms of the * Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Red Hat, Inc. - initial API and implementation ******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.FileChannel; public class Main { private static void copyFile(File source, File target) throws IOException { File file = null; if (source.isDirectory() && source.exists() && target.isDirectory() && target.exists()) { File[] children = source.listFiles(); for (File child : children) { file = target; if (child.isDirectory()) { file = new File(target, child.getName()); if (!file.exists()) file.mkdir(); } copyFile(child, file); } } else {// source is a file if (target.isFile()) { file = target; } else { file = new File(target, source.getName()); } FileChannel out = null; FileChannel in = null; try { if (!file.exists()) { file.createNewFile(); } out = new FileOutputStream(file).getChannel(); in = new FileInputStream(source).getChannel(); in.transferTo(0, in.size(), out); } catch (IOException e) { e.printStackTrace(); throw e; } finally { if (out != null) out.close(); if (in != null) in.close(); } } } private static boolean isFile(URL url) { return "file".equals(url.getProtocol()); } }