Here you can find the source of copyFile(File src, File dest)
Parameter | Description |
---|---|
src | the source file |
dest | the destination directory |
Parameter | Description |
---|---|
IOException | an exception |
public static void copyFile(File src, File dest) throws IOException
//package com.java2s; /*L//from ww w.java 2 s . co m * Copyright The General Hospital Corporation d/b/a Massachusetts General Hospital * * Distributed under the OSI-approved BSD 3-Clause License. * See http://ncip.github.com/digital-model-repository/LICENSE.txt for details. */ 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 src File to dest directory (preserve's src filename: * src.getName()), using Input/Output streams. * * @param src * the source file * @param dest * * the destination directory * @throws IOException */ public static void copyFile(File src, File dest) throws IOException { FileInputStream in = new FileInputStream(src); if (dest.isDirectory()) dest = new File(dest, src.getName()); FileOutputStream out = new FileOutputStream(dest); copyInputStream(in, out); in.close(); out.close(); } private static final void copyInputStream(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int len; while ((len = in.read(buffer)) >= 0) out.write(buffer, 0, len); in.close(); out.close(); } }