Here you can find the source of copyFile(File src, File dest)
Parameter | Description |
---|---|
src | the source file. |
dest | the destination. |
true
if the file was successfully copied, false
otherwise.
public static boolean copyFile(File src, File dest)
//package com.java2s; /******************************************************************************* * Copyright (c) 2000, 2009 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are 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://from ww w. jav a2 s .c om * IBM Corporation - initial API and implementation *******************************************************************************/ import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class Main { /** * Copy a file to another location. * * @param src the source file. * @param dest the destination. * @return <code>true</code> if the file was successfully copied, * <code>false</code> otherwise. */ public static boolean copyFile(File src, File dest) { try { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } return true; } }