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

Parameter

Parameter Description
sourceFile the source file
destinationFile the destination file

Exception

Parameter Description
IOException Signals that an I/O exception has occurred.

Declaration

public static void copyFile(File sourceFile, File destinationFile) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**/*  ww  w.  j a  v  a  2s  .c om*/
     * Copy file.
     *
     * @param sourceFile the source file
     * @param destinationFile the destination file
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void copyFile(File sourceFile, File destinationFile) throws IOException {
        FileInputStream sourceIs = null;
        FileChannel source = null;
        FileOutputStream destinationOs = null;
        FileChannel destination = null;
        try {
            sourceIs = new FileInputStream(sourceFile);
            source = sourceIs.getChannel();
            destinationOs = new FileOutputStream(destinationFile);
            destination = destinationOs.getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (sourceIs != null) {
                sourceIs.close();
            }
            if (destination != null) {
                destination.close();
            }
            if (destinationOs != null) {
                destinationOs.close();
            }
        }
    }
}

Related

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