Here you can find the source of copyFile(File src, File dst)
Parameter | Description |
---|---|
src | The source. It must be a file |
dst | Must not exist as a DIR |
public static void copyFile(File src, File dst)
//package com.java2s; /*//from w w w. ja v a2 s .c om This file is part of the Greenfoot program. Copyright (C) 2005-2009 Poul Henriksen and Michael Kolling This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. This file is subject to the Classpath exception as provided in the LICENSE.txt file that accompanied this code. */ import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Main { /** * Copies the src to dst. Creating parent dirs for dst. If dst exist it * overrides it. * * @param src * The source. It must be a file * @param dst * Must not exist as a DIR */ public static void copyFile(File src, File dst) { if (!src.isFile() || dst.isDirectory()) { return; } dst.getParentFile().mkdirs(); if (dst.exists()) { dst.delete(); } try { BufferedInputStream is = new BufferedInputStream( new FileInputStream(src)); BufferedOutputStream os = new BufferedOutputStream( new FileOutputStream(dst)); byte[] buffer = new byte[8192]; int read = 0; while (read != -1) { os.write(buffer, 0, read); read = is.read(buffer); } os.flush(); is.close(); os.close(); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { } } }