Java FileChannel Copy copyFile(File sourceFile, File destFile)

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

Description

Copy file from a path to another.

License

Open Source License

Parameter

Parameter Description
sourceFile the source file
destFile the destination file

Exception

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

Declaration

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

Method Source Code


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

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  va 2s  .  c  o  m*/
     * Copy file from a path to another.
     *
     * @param sourceFile the source file
     * @param destFile the destination file
     * @throws IOException Signals that an I/O exception has occurred.
     */
    public static void copyFile(File sourceFile, File destFile) throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();
        }
        FileInputStream fin = new FileInputStream(sourceFile);
        try {
            FileOutputStream fout = new FileOutputStream(destFile);
            try {
                FileChannel source = fin.getChannel();
                try {
                    FileChannel destination = fout.getChannel();
                    try {
                        long count = 0;
                        long size = source.size();
                        while ((count += destination.transferFrom(source, count, size - count)) < size)
                            ;
                    } finally {
                        destination.close();
                    }
                } finally {
                    source.close();
                }
            } finally {
                fout.close();
            }
        } finally {
            fin.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 destFile)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File sourceFile, File destinationFile)
  8. copyFile(File sourceFile, File destinationFile)
  9. copyFile(File sourceFile, File destinationFile)