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 src, File dst)
copy File
long p = 0, dp, size;
FileChannel in = null, out = null;
try {
    if (!dst.exists())
        dst.createNewFile();
    in = new FileInputStream(src).getChannel();
    out = new FileOutputStream(dst).getChannel();
    size = in.size();
...
voidcopyFile(File src, File dst)
copies the contents of a file
FileInputStream in = new FileInputStream(src);
FileOutputStream out = new FileOutputStream(dst);
copyChannel(in.getChannel(), src.length(), (WritableByteChannel) out.getChannel());
in.close();
out.close();
voidcopyFile(File src, File dst)
Copy a file to another folder
if (src == null) {
    throw new IllegalArgumentException("File src can't be null!!");
if (dst == null) {
    throw new IllegalArgumentException("File dst can't be null!!");
if (!src.exists()) {
    throw new FileNotFoundException("File " + src.getName() + " can't be found!");
...
voidcopyFile(File src, File dst)
copy File
FileChannel c1 = new RandomAccessFile(src, "r").getChannel();
FileChannel c2 = new RandomAccessFile(dst, "rw").getChannel();
long tCount = 0, size = c1.size();
while ((tCount += c2.transferFrom(c1, 0, size - tCount)) < size) {
c1.close();
c2.force(true);
c2.close();
...
voidcopyFile(File src, File target)
copy File
prepareWrite(target);
try (FileInputStream in = new FileInputStream(src); FileOutputStream out = new FileOutputStream(target)) {
    FileChannel chin = in.getChannel();
    FileChannel chout = out.getChannel();
    chin.transferTo(0, chin.size(), chout);
booleancopyFile(File src, File target)
copy File
boolean flag = false;
FileInputStream in = null;
FileOutputStream out = null;
try {
    in = new FileInputStream(src);
    out = new FileOutputStream(target);
    FileChannel inCh = in.getChannel();
    FileChannel outCh = out.getChannel();
...
voidcopyFile(File srcFile, File destFile, boolean preserveFileDate)
From Apache Commons FileUtils
if (srcFile == null) {
    throw new NullPointerException("Source must not be null");
if (destFile == null) {
    throw new NullPointerException("Destination must not be null");
if (srcFile.exists() == false) {
    throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
...
voidcopyFile(File srcFile, File destFile, boolean preserveFileDate)
Copies a file to a new location.
if (srcFile == null) {
    throw new NullPointerException("Source must not be null");
if (destFile == null) {
    throw new NullPointerException("Destination must not be null");
if (srcFile.exists() == false) {
    throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
...
voidcopyFile(File srcFile, File destFile, boolean preserveFileDate)
copy File
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 {
...
voidcopyFile(File srcFile, File dstFile, boolean overwrite)
copy File
if (dstFile.isDirectory())
    throw new IOException("copyFile: destination is a directory: " + dstFile.getAbsolutePath());
if (dstFile.exists() && !overwrite)
    throw new IOException("copyFile: destination file already exists: " + dstFile.getAbsolutePath());
FileChannel srcChannel = null;
FileChannel dstChannel = null;
try {
    srcChannel = new FileInputStream(srcFile).getChannel();
...