Java File Copy nio copy(File sourceFile, File targetFile)

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

Description

copy

License

LGPL

Declaration

public static void copy(File sourceFile, File targetFile) throws IOException 

Method Source Code


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

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    public static void copy(File sourceFile, File targetFile) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel targetChannel = null;
        try {/*from   w w  w .  j  a  va2  s.  com*/
            sourceChannel = new FileInputStream(sourceFile).getChannel();
            targetChannel = new FileOutputStream(targetFile).getChannel();
            targetChannel.transferFrom(sourceChannel, 0, sourceChannel.size());
        } finally {
            if (sourceChannel != null)
                closeQuietly(sourceChannel);
            if (targetChannel != null)
                closeQuietly(targetChannel);
        }
    }

    public static void closeQuietly(Closeable closeable) {
        try {
            closeable.close();
        } catch (IOException ignore) {
            //
        }
    }
}

Related

  1. copy(File source, File target)
  2. copy(File source, File target, FilenameFilter filter)
  3. copy(File sourceFile, File destFile)
  4. copy(File sourceFile, File destFile)
  5. copy(File sourceFile, File destinationFile)
  6. copy(File src, File dest)
  7. copy(File src, File dest)
  8. copy(File src, File dest)
  9. copy(File src, File dest)