Java File Copy nio copyFile(FileChannel srcFc, File dstFile)

Here you can find the source of copyFile(FileChannel srcFc, File dstFile)

Description

copy File

License

LGPL

Declaration

public static long copyFile(FileChannel srcFc, File dstFile) throws IOException 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {

    public static long copyFile(FileChannel srcFc, File dstFile) throws IOException {
        long r = 0;
        FileOutputStream fos = new FileOutputStream(dstFile);
        try {/* w w  w  .j  a  v a2s  .  c  om*/
            ByteBuffer bb = ByteBuffer.allocate(32768);
            for (int n; (n = srcFc.read(bb)) != -1;) {
                bb.flip();
                fos.write(bb.array(), 0, bb.limit());
                bb.clear();
                r += n;
            }
        } finally {
            fos.close();
        }
        return r;
    }

    public static long copyFile(File srcFile, File dstFile) throws IOException {
        FileInputStream fis = new FileInputStream(srcFile);
        try {
            return copyFile(fis.getChannel(), dstFile);
        } finally {
            fis.close();
        }
    }
}

Related

  1. copy(File srcFileOrDir, File targetFileOrDir)
  2. copy(File toFile, File fromFile)
  3. copyFile(File f1, File f2)
  4. copyFile(File source, File dest, boolean visibleFilesOnly)
  5. copyFile(File src, File dest)
  6. copyFile(final File source, final File dest)
  7. copyFile(final File source, final File target)
  8. copyFile(final File source, final File target)
  9. copyFileByMapped(String sourcePath, String targetPath)