Java Utililty Methods FileChannel Copy

List of utility methods to do FileChannel Copy

Description

The list of methods to do FileChannel Copy are organized into topic(s).

Method

voidcopyTree(final File source, final File dest)
This method is used to copy an entire file tree from one location to another.
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);
...
voidcopyURLToFile(URL in, File out)
copy URL To File
ReadableByteChannel inChannel = Channels.newChannel(in.openStream());
FileChannel outChannel = new FileOutputStream(out).getChannel();
try {
    outChannel.transferFrom(inChannel, 0, 1 << 24);
} catch (IOException e) {
    throw e;
} finally {
    if (inChannel != null)
...
voidcopyUsingNIO(String src, String dest)
Copy using nio.
File sourceFile = new File(src);
File destFile = new File(dest);
copyUsingNIO(sourceFile, destFile);
voidcopyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend)
copy With Channels
ensureTargetDirectoryExists(aTargetFile.getParentFile());
FileChannel inChannel = null;
FileChannel outChannel = null;
FileInputStream inStream = null;
FileOutputStream outStream = null;
try {
    try {
        inStream = new FileInputStream(aSourceFile);
...
voiddirectoryCopy(URL source, URL destination)
Copies the contents for source directory to destination directory.
checkCanCopy(source, destination);
source = getFileURL(source);
destination = getFileURL(destination);
File dstFile = new File(destination.getFile());
if (!dstFile.exists() && !dstFile.mkdir()) {
    return;
if ("file".equals(source.getProtocol())) {
...
voiddoCopyDirectory(File srcDir, File destDir, boolean preserveFileDate, List exclusionList)
Internal copy directory method.
File[] files = srcDir.listFiles();
if (files == null) { 
    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);
if (preserveFileDate) {
    destDir.setLastModified(srcDir.lastModified());
voiddoCopyFile(File srcFile, File destFile, boolean preserveFileDate)
From Apache Commons FileUtils
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 {
...
voiddoCopyFile(File srcFile, File destFile, boolean preserveFileDate)
Internal copy file method.
if (destFile.exists() && destFile.isDirectory()) {
    throw new IOException("Destination '" + destFile + "' exists but is a directory");
FileChannel input = new FileInputStream(srcFile).getChannel();
try {
    FileChannel output = new FileOutputStream(destFile).getChannel();
    try {
        output.transferFrom(input, 0, input.size());
...
voidfCopy(FileInputStream src, File dest)
f Copy
if (null == src || null == dest) {
FileChannel srcChannel = src.getChannel();
FileChannel dstChannel = new FileOutputStream(dest).getChannel();
dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
srcChannel.close();
dstChannel.close();
voidfileChannelCopy(File src, File dest)
file Channel Copy
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null, out = null;
try {
    fi = new FileInputStream(src);
    fo = new FileOutputStream(dest);
    in = fi.getChannel();
    out = fo.getChannel();
...