Java FileChannel Copy copyFile(File sourceFile, File targetFile)

Here you can find the source of copyFile(File sourceFile, File targetFile)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, File targetFile) 

Method Source Code

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

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

import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File sourceFile, File targetFile) {
        FileInputStream source;/*from  w w  w  .  ja  v  a 2  s.  c om*/
        FileOutputStream destination;
        try {
            source = new FileInputStream(sourceFile);
            destination = new FileOutputStream(targetFile);
            FileChannel sourceFileChannel = source.getChannel();
            FileChannel destinationFileChannel = destination.getChannel();

            long size = sourceFileChannel.size();
            sourceFileChannel.transferTo(0, size, destinationFileChannel);
            sourceFileChannel.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

  1. copyFile(File sourceFile, File destFile)
  2. copyFile(File sourceFile, File destinationFile)
  3. copyFile(File sourceFile, File destinationFile)
  4. copyFile(File sourceFile, File destinationFile)
  5. copyFile(File sourceFile, File targetFile)
  6. copyFile(File sourceFile, File targetFile)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)