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

booleancopyFile(File in, File out)
Copies a file.
if (!in.exists()) {
    System.err.println("File '" + in.getName() + "' does not exist.");
    return false;
boolean success = false;
try {
    FileChannel inChannel = new FileInputStream(in).getChannel();
    FileChannel outChannel = new FileOutputStream(out).getChannel();
...
voidcopyFile(File in, File out)
Copy a file from one location to another.
FileChannel inChannel = new FileInputStream(in).getChannel();
FileChannel outChannel = new FileOutputStream(out).getChannel();
long dataTransferred = 0;
long blockSize = 1024 * 1024; 
try {
    while (dataTransferred < inChannel.size()) {
        long dataLeft = inChannel.size() - dataTransferred;
        long toSend = (dataLeft < blockSize ? dataLeft : blockSize);
...
voidcopyFile(File in, File out)
Copy the content of a file to another.
copyFile(in, out, true);
voidcopyFile(File in, File out)
Copies one file to another.
FileChannel sourceChannel = new FileInputStream(in).getChannel();
FileChannel destinationChannel = new FileOutputStream(out).getChannel();
sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
sourceChannel.close();
destinationChannel.close();
voidcopyFile(File in, File out)
copy File
copyFile(in, out, CHANNEL_MAX_COUNT);
voidcopyFile(File in, File out)
copy File
FileChannel sourceChannel = null;
FileChannel destinationChannel = null;
try {
    sourceChannel = new FileInputStream(in).getChannel();
    destinationChannel = new FileOutputStream(out).getChannel();
    sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
} finally {
    try {
...
voidcopyFile(File in, File out)
Kopieert een file, NIO
try (FileInputStream fin = new FileInputStream(in);
        FileChannel inChannel = fin.getChannel();
        FileOutputStream fout = new FileOutputStream(out);
        FileChannel outChannel = fout.getChannel()) {
    inChannel.transferTo(0, inChannel.size(), outChannel);
} catch (IOException e) {
    throw e;
voidcopyfile(File infile, File outfile)
copyfile
FileChannel in = channel(infile, false);
FileChannel out = channel(outfile, true);
for (long offset = 0; offset < in.size();) {
    long n = in.transferTo(offset, in.size() - offset, out);
    if (n <= 0) {
        throw new IOException("Failed transfer to channel.");
    offset += n;
...
voidcopyFile(File inFile, File outFile)
Copy a file using java.nio
copyFile(inFile, outFile, false);
voidcopyFile(File inputFile, File outputFile)
Copies a file to the specified location.
if (inputFile.isDirectory()) {
    throw new IOException("Cannot copy file: " + inputFile + " as this is a directory");
if (outputFile.isDirectory()) {
    outputFile = new File(outputFile, inputFile.getName());
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
...