Java Directory Copy copyDirectory(File sourceLocation, File targetLocation)

Here you can find the source of copyDirectory(File sourceLocation, File targetLocation)

Description

copy Directory

License

Open Source License

Declaration

public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException 

Method Source Code


//package com.java2s;
/* /*from   ww  w.  j a  va 2  s .com*/
 * Siberia utilities : siberia plugin providing severall utilities classes
 *
 * Copyright (C) 2008 Alexis PARIS
 * Project Lead:  Alexis Paris
 *
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 *
 * This library 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library;
 * if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
 */

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException {
        copyDirectory(sourceLocation, targetLocation, null);
    }

    public static void copyDirectory(File sourceLocation, File targetLocation, FileFilter filter)
            throws IOException {
        if (sourceLocation != null) {
            if (sourceLocation.exists()) {
                if (sourceLocation.isDirectory()) {
                    if (!targetLocation.exists()) {
                        targetLocation.mkdirs();
                    }

                    File[] children = sourceLocation.listFiles(filter);
                    for (int i = 0; i < children.length; i++) {
                        copyDirectory(children[i], new File(targetLocation, children[i].getName()), null);
                    }
                } else {
                    if (!targetLocation.exists())
                        targetLocation.createNewFile();

                    InputStream in = new FileInputStream(sourceLocation);
                    OutputStream out = new FileOutputStream(targetLocation);

                    // Copy the bits from instream to outstream
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    in.close();
                    out.close();
                }
            }
        }
    }
}

Related

  1. copyDirectory(File sourceDir, File destDir)
  2. copyDirectory(File sourceDir, File destDir)
  3. copyDirectory(File sourceDir, File destinationDir)
  4. copyDirectory(File sourceLocation, File targetLocation)
  5. copyDirectory(File sourceLocation, File targetLocation)
  6. copyDirectory(File sourceLocation, File targetLocation)
  7. copyDirectory(File sourceLocation, File targetLocation, boolean overwrite)
  8. copyDirectory(File sourceLocation, File targetLocation, int bufferSize)
  9. copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)