Here you can find the source of copyFile(final File src, final File dest)
Parameter | Description |
---|---|
src | The Source File or Directory to be copied to the dest location. |
dest | The Destination location to copy the Source content to. |
Parameter | Description |
---|---|
IOException | Thrown if the destination location doesn't exist or there write permissions for the destination. |
public static void copyFile(final File src, final File dest) throws IOException
//package com.java2s; 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 { /**//ww w . j a va 2 s. c o m * Copies a file or directory from one location to another location on a local system.<br /><br /> Note: The destination location must * exist, otherwise a IOException will be thrown. * * @param src The Source File or Directory to be copied to the dest location. * @param dest The Destination location to copy the Source content to. * @throws IOException Thrown if the destination location doesn't exist or there write permissions for the destination. */ public static void copyFile(final File src, final File dest) throws IOException { if (src.isDirectory()) { // if the directory does not exist, create it if (!dest.exists()) { dest.mkdir(); } // get all the directory contents final String files[] = src.list(); // Copy all the folders/files in the directory for (final String file : files) { // construct the src and dest file structure final File srcFile = new File(src, file); final File destFile = new File(dest, file); // recursive copy copyFile(srcFile, destFile); } } else { // if its a file, then copy it final InputStream in = new FileInputStream(src); final OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; // copy the file contents while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } // Clean up in.close(); out.close(); } } }