Java FileChannel Copy copyAFile(File source, File target)

Here you can find the source of copyAFile(File source, File target)

Description

copy A File

License

Open Source License

Declaration

public static void copyAFile(File source, File target) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyAFile(File source, File target) throws IOException {
        FileInputStream sourceInputStream = null;
        FileInputStream targetInputStream = null;
        try {/*  w  w  w. j  a  va2s. c om*/
            sourceInputStream = new FileInputStream(source);
            targetInputStream = new FileInputStream(target);
            FileChannel sourceChannel = sourceInputStream.getChannel();
            FileChannel targetChannel = targetInputStream.getChannel();
            sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
            sourceChannel.close();
            targetChannel.close();
        } finally {
            if (sourceInputStream != null) {
                sourceInputStream.close();
            }
            if (targetInputStream != null) {
                targetInputStream.close();
            }
        }
    }
}

Related

  1. copy(String source, String destination, boolean recursive)
  2. copy(String sourceFile, String targetFile)
  3. copy0(File src, File dest)
  4. copy12(File file1, File file2)
  5. copy2(String sourceFileName, String destFileName)
  6. copyAll(File source, File targetDir)
  7. copyAndReplace(File from, File to)
  8. copyChannel(ReadableByteChannel in, long length, FileChannel out)
  9. copyFile(File aFromFile, String aToFilename)