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

voidcopy(String source, String destination)
copy
File s = new File(source);
File d = new File(destination);
FileInputStream ss = new FileInputStream(s);
FileOutputStream ds = new FileOutputStream(d);
FileChannel sourceFileChannel = ss.getChannel();
FileChannel destinationFileChannel = ds.getChannel();
long size = sourceFileChannel.size();
sourceFileChannel.transferTo(0, size, destinationFileChannel);
...
voidcopy(String source, String destination, boolean recursive)
Copies a source file to a destination file or directory OR Copies a source directory to a destination directory.
File sourceFile = new File(source);
File destinationFile = new File(destination);
copy(sourceFile, destinationFile, recursive);
voidcopy(String sourceFile, String targetFile)
Copies one file to another.
FileChannel sourceChannel = new FileInputStream(sourceFile).getChannel();
FileChannel targetChannel = new FileOutputStream(targetFile).getChannel();
targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
sourceChannel.close();
targetChannel.close();
longcopy0(File src, File dest)
copy the src file to dest.
long size = 0;
InputStream in = null;
OutputStream out = null;
try {
    in = new BufferedInputStream(new FileInputStream(src));
    out = new BufferedOutputStream(new FileOutputStream(dest));
    size = copy(in, out);
} finally {
...
voidcopy12(File file1, File file2)
copy
try {
    mylogger.log(Level.INFO, "Copy12:[0}] -> [{1}]{ ...",
            new Object[] { file1.getAbsolutePath(), file2.getAbsolutePath() });
    FileChannel in = (new FileInputStream(file1)).getChannel();
    FileChannel out = (new FileOutputStream(file2)).getChannel();
    in.transferTo(0, file1.length(), out);
    in.close();
    out.close();
...
voidcopy2(String sourceFileName, String destFileName)
copy
FileInputStream in = new FileInputStream(sourceFileName);
FileOutputStream out = new FileOutputStream(destFileName);
FileChannel inC = in.getChannel();
FileChannel outC = out.getChannel();
while (inC.position() < inC.size()) {
    inC.transferTo(inC.position(), MB, outC);
    inC.position(inC.position() + MB);
inC.close();
outC.close();
in.close();
out.close();
voidcopyAFile(File source, File target)
copy A File
FileInputStream sourceInputStream = null;
FileInputStream targetInputStream = null;
try {
    sourceInputStream = new FileInputStream(source);
    targetInputStream = new FileInputStream(target);
    FileChannel sourceChannel = sourceInputStream.getChannel();
    FileChannel targetChannel = targetInputStream.getChannel();
    sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
...
voidcopyAll(File source, File targetDir)
copy All
if (!targetDir.isDirectory()) {
    throw new RuntimeException("Cannot copy files to a file that is not a directory.");
File target = new File(targetDir, source.getName());
if (source.isDirectory()) {
    if (!target.mkdir()) {
        throw new RuntimeException("Cannot create directory " + target.getAbsolutePath());
    for (File f : source.listFiles()) {
        copyAll(f, target);
} else {
    copyFile(source, target);
voidcopyAndReplace(File from, File to)
copy And Replace
if (to.exists()) {
    to.delete();
to.createNewFile();
FileChannel inChannel = new FileInputStream(from).getChannel();
FileChannel outChannel = new FileOutputStream(to).getChannel();
try {
    inChannel.transferTo(0, inChannel.size(), outChannel);
...
longcopyChannel(ReadableByteChannel in, long length, FileChannel out)
copy Channel
if (length < 0L || length >= Integer.MAX_VALUE)
    return out.transferFrom(in, 0L, length);
long result = 0L;
while (result < length)
    result += out.transferFrom(in, result, length - result);
return result;