Java FileChannel Copy copyFile(File sourceFile, File destFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, File destFile) 

Method Source Code


//package com.java2s;
import java.io.*;
import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File sourceFile, File destFile) {
        try {//  ww  w.j ava  2 s  .  com
            if (!destFile.exists()) {
                destFile.createNewFile();
            }
            FileChannel source = null;
            FileChannel destination = null;

            try {
                source = new FileInputStream(sourceFile).getChannel();
                destination = new FileOutputStream(destFile).getChannel();
                destination.transferFrom(source, 0, source.size());
            } finally {
                close(source);
                close(destination);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void close(Closeable closeable) {
        try {
            closeable.close();
        } catch (Exception e) {
            // Ignore!
        }
    }
}

Related

  1. copyFile(File source, File target)
  2. copyFile(File source, File target)
  3. copyFile(File source, File target)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File sourceFile, File destFile)
  8. copyFile(File sourceFile, File destFile)
  9. copyFile(File sourceFile, File destFile)