Description
Internal copy directory method.
License
Apache License
Parameter
Parameter | Description |
---|
srcDir | the validated source directory, must not be <code>null</code> |
destDir | the validated destination directory, must not be <code>null</code> |
preserveFileDate | whether to preserve the file date |
exclusionList | List of files and directories to exclude from the copy, may be null |
Exception
Parameter | Description |
---|
IOException | if an error occurs |
Declaration
private static void doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate,
List<String> exclusionList) throws IOException
Method Source Code
//package com.java2s;
/* GROOVE: GRaphs for Object Oriented VErification
* Copyright 2003--2010 University of Twente
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*
* $Id: Util.java 5482 2014-07-20 22:25:43Z rensink $
*///from www. j av a2 s .co m
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.List;
public class Main {
/**
* The number of bytes in a 50 MB.
*/
private static final long FIFTY_MB = 1024 * 1024 * 50;
/**
* Internal copy directory method.
*
* @param srcDir the validated source directory, must not be <code>null</code>
* @param destDir the validated destination directory, must not be <code>null</code>
* @param preserveFileDate whether to preserve the file date
* @param exclusionList List of files and directories to exclude from the copy, may be null
* @throws IOException if an error occurs
* @since Commons IO 1.1
*/
private static void doCopyDirectory(File srcDir, File destDir, boolean preserveFileDate,
List<String> exclusionList) throws IOException {
// recurse
File[] files = srcDir.listFiles();
if (files == null) { // null if security restricted
throw new IOException("Failed to list contents of " + srcDir);
}
if (destDir.exists()) {
if (destDir.isDirectory() == false) {
throw new IOException("Destination '" + destDir + "' exists but is not a directory");
}
} else {
if (destDir.mkdirs() == false) {
throw new IOException("Destination '" + destDir + "' directory cannot be created");
}
}
if (destDir.canWrite() == false) {
throw new IOException("Destination '" + destDir + "' cannot be written to");
}
for (File file : files) {
File copiedFile = new File(destDir, file.getName());
if (exclusionList == null || !exclusionList.contains(file.getCanonicalPath())) {
if (file.isDirectory()) {
doCopyDirectory(file, copiedFile, preserveFileDate, exclusionList);
} else {
doCopyFile(file, copiedFile, preserveFileDate);
}
}
}
// Do this last, as the above has probably affected directory metadata
if (preserveFileDate) {
destDir.setLastModified(srcDir.lastModified());
}
}
/**
* Internal copy file method.
*
* @param srcFile the validated source file, must not be <code>null</code>
* @param destFile the validated destination file, must not be <code>null</code>
* @param preserveFileDate whether to preserve the file date
* @throws IOException if an error occurs
*/
private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {
if (destFile.exists() && destFile.isDirectory()) {
throw new IOException("Destination '" + destFile + "' exists but is a directory");
}
FileInputStream fis = null;
FileOutputStream fos = null;
FileChannel input = null;
FileChannel output = null;
try {
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
input = fis.getChannel();
output = fos.getChannel();
long size = input.size();
long pos = 0;
long count = 0;
while (pos < size) {
count = (size - pos) > FIFTY_MB ? FIFTY_MB : (size - pos);
pos += output.transferFrom(input, pos, count);
}
} finally {
output.close();
fos.close();
input.close();
fis.close();
}
if (srcFile.length() != destFile.length()) {
throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "'");
}
if (preserveFileDate) {
destFile.setLastModified(srcFile.lastModified());
}
}
}
Related
- copyTree(final File source, final File dest)
- copyURLToFile(URL in, File out)
- copyUsingNIO(String src, String dest)
- copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
- directoryCopy(URL source, URL destination)
- doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
- doCopyFile(File srcFile, File destFile, boolean preserveFileDate)
- fCopy(FileInputStream src, File dest)
- fileChannelCopy(File src, File dest)