Here you can find the source of copyDirectory(final File sourceDir, final File destDir)
Parameter | Description |
---|---|
src | source. |
dest | destination. |
public static void copyDirectory(final File sourceDir, final File destDir) throws IOException
//package com.java2s; /**/* w w w.j ava 2s . c om*/ * Copyright (c) {2011} {meter@rbtsb.com} { * individual contributors as indicated by the @authors tag}. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Private License v1.0 * which accompanies this distribution, and is available at * http://www.rbtsb.com */ 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 { /** * @param src * source. * @param dest * destination. */ public static void copyDirectory(final File sourceDir, final File destDir) throws IOException { if (!destDir.exists()) { destDir.mkdir(); } File[] children = sourceDir.listFiles(); for (File sourceChild : children) { String name = sourceChild.getName(); File destChild = new File(destDir, name); if (sourceChild.isDirectory()) { copyDirectory(sourceChild, destChild); } else { copyFile(sourceChild, destChild); } } } /** * @param source * source. * @param dest * destination. * @throws Exception . */ public static void copyFile(final File source, final File dest) throws IOException { if (!dest.getParentFile().exists()) { String dir = dest.getParentFile().toString(); new File(dir).mkdir(); } InputStream in = null; OutputStream out = null; if (source.exists()) { try { 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); } } finally { if (in != null) in.close(); if (out != null) out.close(); } } } }