Here you can find the source of copyFile(File source, File dest)
Parameter | Description |
---|---|
source | a parameter |
dest | destination directory or destination file |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(File source, File dest) throws IOException
//package com.java2s; /******************************************************************************* * Copyright 2008(c) The OBiBa Consortium. All rights reserved. * /*from ww w . j ava 2 s .com*/ * This program and the accompanying materials * are made available under the terms of the GNU Public License v3.0. * * 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.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Copy a normal file to another. * @param source * @param dest destination directory or destination file * @throws IOException */ public static void copyFile(File source, File dest) throws IOException { if (source == null) throw new IllegalArgumentException("source cannot be null"); if (dest == null) throw new IllegalArgumentException("dest cannot be null"); if (dest.isDirectory()) { dest = new File(dest, source.getName()); } if (!dest.exists()) { if (dest.createNewFile() == false) { throw new IOException("Cannot create destination file " + dest.getAbsolutePath()); } } InputStream in = null; OutputStream out = null; try { in = new FileInputStream(source); out = new FileOutputStream(dest); byte[] buf = new byte[4096]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } finally { try { if (in != null) in.close(); } catch (IOException e) { // ignore } try { if (out != null) out.close(); } catch (IOException e) { // ignore } } } }