Java FileChannel Copy copyFile(File sourceFile, File destinationFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File sourceFile, File destinationFile) 

Method Source Code

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

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File sourceFile, File destinationFile) {
        copyFile(sourceFile, destinationFile, true);
    }//from w ww . jav a 2s. c om

    public static void copyFile(File sourceFile, File destinationFile, boolean overwrite) {
        try {
            if (sourceFile.exists()) {
                if (!overwrite && destinationFile.exists()) {
                    System.out.println("Destination file exists\n" + sourceFile.getAbsolutePath());
                    return;
                }
                if (!destinationFile.exists()) {
                    destinationFile.createNewFile();
                }
                FileChannel sourceStream = null, destinationStream = null;
                try {
                    sourceStream = new FileInputStream(sourceFile).getChannel();
                    destinationStream = new FileOutputStream(destinationFile).getChannel();
                    destinationStream.transferFrom(sourceStream, 0, sourceStream.size());
                } finally {
                    if (sourceStream != null) {
                        sourceStream.close();
                    }
                    if (destinationStream != null) {
                        destinationStream.close();
                    }
                }
            } else {
                System.out.println("Source file does not exist\n" + sourceFile.getAbsolutePath());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related

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