Description
Copy a file or directory to another.
License
Open Source License
Parameter
Parameter | Description |
---|
sourceLocation | the source |
targetLocation | the destination |
overwrite | If true, overwrite files in the targetLocation |
Exception
Parameter | Description |
---|
IOException | an exception |
Declaration
public static void copyDirectory(File sourceLocation,
File targetLocation, boolean overwrite) throws IOException
Method Source Code
//package com.java2s;
/*//from w w w . j a v a 2s .com
* Copyright (c) 2010 The Regents of the University of California.
* All rights reserved.
*
* '$Author: crawl $'
* '$Date: 2012-08-03 09:30:04 -0700 (Fri, 03 Aug 2012) $'
* '$Revision: 30343 $'
*
* Permission is hereby granted, without written agreement and without
* license or royalty fees, to use, copy, modify, and distribute this
* software and its documentation for any purpose, provided that the above
* copyright notice and the following two paragraphs appear in all copies
* of this software.
*
* IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
* FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
* ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
* THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
* PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
* CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
* ENHANCEMENTS, OR MODIFICATIONS.
*
*/
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 {
/** Copy a file or directory to another. If the source is a directory,
* the contents are recursively copied.
* @param sourceLocation the source
* @param targetLocation the destination
* @param overwrite If true, overwrite files in the targetLocation
* @throws IOException
*/
public static void copyDirectory(File sourceLocation,
File targetLocation, boolean overwrite) throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists()) {
boolean created = targetLocation.mkdirs();
if (!created) {
System.out
.println("FileUtil copyDirectory ERROR couldn't mkdirs:"
+ targetLocation);
// if we couldn't create the directories, stop copying
return;
}
}
String[] children = sourceLocation.list();
for (int i = 0; i < children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]), overwrite);
}
} else if (overwrite || !targetLocation.exists()) {
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();
//set executable property
if (sourceLocation.canExecute())
targetLocation.setExecutable(true);
}
}
}
Related
- copyDirectory(File sourceDir, File destinationDir)
- copyDirectory(File sourceLocation, File targetLocation)
- copyDirectory(File sourceLocation, File targetLocation)
- copyDirectory(File sourceLocation, File targetLocation)
- copyDirectory(File sourceLocation, File targetLocation)
- copyDirectory(File sourceLocation, File targetLocation, int bufferSize)
- copyDirectory(File sourceLocation, File targetLocation, String[] fileExtensions)