Here you can find the source of copyFile(File source, File dest)
Parameter | Description |
---|---|
source | a parameter |
dest | a parameter |
public static void copyFile(File source, File dest)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010 Conselleria de Infraestructuras y Transporte, Generalitat * de la Comunitat Valenciana . 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 * // w w w . j av a 2 s. c o m * Contributors: H?ctor Iturria (Prodevelop) ? Initial implementation. * ******************************************************************************/ 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 { /** * Copies the source file to the dest file * * @param source * @param dest */ public static void copyFile(File source, File dest) { InputStream in = null; OutputStream out = null; try { if (!dest.exists()) { dest.createNewFile(); } in = new FileInputStream(source); out = new FileOutputStream(dest); byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } } catch (IOException e) { System.out.print("Something went wrong while copying the file: " + e.getMessage()); } finally { try { in.close(); out.close(); } catch (IOException e) { e.printStackTrace(); } } } }