Here you can find the source of copyTree(final File source, final File dest)
Parameter | Description |
---|---|
source | The source directory to be copied. |
dest | The destination directory to receive the contents of the copy. |
Parameter | Description |
---|---|
IOException | if the copy fails |
public static void copyTree(final File source, 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.nio.channels.FileChannel; public class Main { /**//from w ww. java 2 s .c o m * This method is used to copy an entire file tree from one location to another. This is a deep copy. * * @param source * The source directory to be copied. * @param dest * The destination directory to receive the contents of the copy. * @throws IOException * if the copy fails */ public static void copyTree(final File source, final File dest) throws IOException { if (source != null && dest != null && source.isDirectory() && dest.isDirectory()) { String[] children = source.list(); for (String child : children) { File in = new File(source, child); File out = new File(dest, child); if (in.isDirectory()) { out.mkdir(); copyTree(in, out); } else { copyFile(in, out); } } } } /** * This method can be used to copy a file from one place to another. * * @param input * The file to be read * @param output * The file to be written to * @throws IOException * if anything fails. */ public static void copyFile(final File input, final File output) throws IOException { FileInputStream in = null; FileOutputStream out = null; try { in = new FileInputStream(input); out = new FileOutputStream(output); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); long size = inChannel.size(); inChannel.transferTo(0L, size, outChannel); } finally { try { if (in != null) { in.close(); } } catch (IOException e) { /* ignore */ } try { if (out != null) { out.close(); } } catch (IOException e) { /* ignore */ } } } }