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

voidfileCopy(String sourceFolder, String destinationFolder)
Copies all the files from the source folder to the destination folder.
File source = new File(sourceFolder);
FileFilter filter = new FileFilter() {
    @Override
    public boolean accept(File pathname) {
        if (pathname.isFile()) {
            if (pathname.getName().indexOf("jpg") != -1 || pathname.getName().indexOf("jpeg") != -1
                    || pathname.getName().indexOf("png") != -1)
                return true;
...
longfileStreamCopy(FileInputStream in, FileOutputStream out, boolean closeOutput)
Optimized copy for file streams using java.nio channels.
FileChannel src = in.getChannel();
FileChannel dst = out.getChannel();
long pos = 0;
long n;
try {
    while ((n = dst.transferFrom(src, pos, NIO_CHANNEL_CHUNK_SIZE)) > 0) {
        pos += n;
} finally {
    src.close();
    if (closeOutput) {
        dst.close();
return pos;
voidnioCopy(File source, File target, FilenameFilter filter)
Copy file or directory to the specified destination.
copy(source, target, filter, true, true);
voidnioCopy(FileOutputStream fos, FileInputStream fis)
Copy a single file using NIO.
FileChannel outChannel = fos.getChannel();
FileChannel inChannel = fis.getChannel();
long length = inChannel.size();
long offset = 0;
while (true) {
    long remaining = length - offset;
    long toTransfer = remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE;
    long transferredBytes = inChannel.transferTo(offset, toTransfer, outChannel);
...
voidnioCopyFile(File source, File target, boolean replaceIfExists)
nio Copy File
if (!target.createNewFile()) 
    if (target.exists() && !replaceIfExists) {
        throw new IOException(String.format("File '%s' already exists. ", target.getAbsolutePath()));
FileInputStream sourceStream = null;
FileOutputStream targetStream = null;
...
longtouchcopy(File src, File dest)
touchcopy
FileChannel in = null;
FileChannel out = null;
long date = src.lastModified();
try {
    in = new FileInputStream(src).getChannel();
    out = new FileOutputStream(dest).getChannel();
    return in.transferTo(0, in.size(), out);
} finally {
...
booleantryCopyFile(File sourceLocation, File targetLocation)
try Copy File
try {
    copyFile(sourceLocation, targetLocation);
    return true;
} catch (IOException ex) {
    ex.printStackTrace();
return false;