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

voidcopyFile(File source, File destination, boolean overwrite)
Copies a file.
FileChannel inputChannel = null;
FileChannel outputChannel = null;
if (destination.exists()) {
    if (overwrite) {
        if (!destination.delete()) {
            throw new IOException("Unable to delete the destination file.");
    } else {
...
booleancopyFile(File source, File destination, long chunkSize, boolean overwrite)
copy File
if (destination.exists() && !overwrite) {
    return false;
} else if (destination.exists() && !destination.canWrite()) {
    return false;
} else {
    FileChannel sourceChannel = null;
    FileChannel destinationChannel = null;
    FileInputStream fis = null;
...
voidcopyFile(File source, File target)
A raw file copy function -- this is not public since no error checks are made as to the consistency of the file being copied.
FileChannel sourceChannel = new FileInputStream(source).getChannel();
FileChannel targetChannel = new FileOutputStream(target).getChannel();
long pos = 0;
long toCopy = sourceChannel.size();
while (toCopy > 0) {
    long bytes = sourceChannel.transferTo(pos, toCopy, targetChannel);
    pos += bytes;
    toCopy -= bytes;
...
voidcopyFile(File source, File target)
copy File
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
    inChannel = new FileInputStream(source).getChannel();
    outChannel = new FileOutputStream(target).getChannel();
    long fileSize = inChannel.size();
    long position = 0;
    while (position < fileSize) {
...
voidcopyFile(File source, File target)
copy File
File file = null;
if (source.isDirectory() && source.exists() && target.isDirectory() && target.exists()) {
    File[] children = source.listFiles();
    for (File child : children) {
        file = target;
        if (child.isDirectory()) {
            file = new File(target, child.getName());
            if (!file.exists())
...
voidcopyFile(File source, File target)
copy File
InputStream is = null;
try {
    is = new FileInputStream(source);
} catch (FileNotFoundException e) {
    e.printStackTrace();
copyFile((FileInputStream) is, target);
voidcopyFile(File source, File target)
Inspired by http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java/115086#115086
if (!target.exists()) {
    target.createNewFile();
FileChannel sourceChannel = null;
FileChannel targetChannel = null;
try {
    sourceChannel = new FileInputStream(source).getChannel();
    targetChannel = new FileOutputStream(target).getChannel();
...
voidcopyFile(File sourceFile, File destFile)
copy File
FileInputStream in = null;
FileOutputStream out = null;
FileChannel source = null;
FileChannel destination = null;
try {
    destFile.createNewFile();
    in = new FileInputStream(sourceFile);
    out = new FileOutputStream(destFile);
...
voidcopyFile(File sourceFile, File destFile)
copy File
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
    try {
        destFile.createNewFile();
    } catch (IOException ioe) {
        throw new RuntimeException("Unable to create file " + destFile.getAbsolutePath(), ioe);
FileChannel source = null;
FileChannel destination = null;
try {
    source = new FileInputStream(sourceFile).getChannel();
    destination = new FileOutputStream(destFile).getChannel();
    destination.transferFrom(source, 0, source.size());
} catch (IOException ioe) {
    throw new RuntimeException(
            "Unable to copy " + sourceFile.getAbsolutePath() + " to " + destFile.getAbsolutePath(), ioe);
} finally {
    if (source != null) {
        try {
            source.close();
        } catch (IOException e) {
    if (destination != null) {
        try {
            destination.close();
        } catch (IOException e) {
voidcopyFile(File sourceFile, File destFile)
copy File
try {
    if (!destFile.exists()) {
        destFile.createNewFile();
    FileChannel source = null;
    FileChannel destination = null;
    try {
        source = new FileInputStream(sourceFile).getChannel();
...