Here you can find the source of copyFile(File src, File dest)
Parameter | Description |
---|---|
src | An existing file to copy, must not be <code>null</code>. |
dest | The new file, must not be <code>null</code> and exist. |
Parameter | Description |
---|---|
IOException | If copying is failed. |
public static void copyFile(File src, File dest) throws IOException
//package com.java2s; /*/*www. ja v a2 s .com*/ * casmi * http://casmi.github.com/ * Copyright (C) 2011, Xcoo, Inc. * * casmi is free software: you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as published by * the Free Software Foundation, either version 3 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser 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.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class Main { /** * Copies a file to a new location preserving the file date. * * @param src * An existing file to copy, must not be <code>null</code>. * @param dest * The new file, must not be <code>null</code> and exist. * * @throws IOException * If copying is failed. */ public static void copyFile(File src, File dest) throws IOException { if (src == null) throw new NullPointerException("Source must not be null"); if (dest == null) throw new NullPointerException("Destination must not be null"); if (!src.exists()) throw new FileNotFoundException("Source '" + src + "' does not exist"); if (!src.isFile()) throw new IOException("Source '" + src + "' is not a file"); if (dest.exists()) throw new IOException("Destination '" + dest + "' is already exists"); FileChannel in = null, out = null; try { in = new FileInputStream(src).getChannel(); out = new FileOutputStream(dest).getChannel(); in.transferTo(0, in.size(), out); } finally { try { in.close(); out.close(); } catch (IOException e) { // Ignore. } } if (src.length() != dest.length()) { throw new IOException("Failed to copy full contents from '" + src + "' to '" + dest + "'"); } dest.setLastModified(src.lastModified()); } }