Java FileChannel Copy copyFile(File source, File dest)

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

Description

copy File

License

Apache License

Declaration

public static void copyFile(File source, File dest) 

Method Source Code

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

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import java.nio.channels.FileChannel;

public class Main {
    public static void copyFile(File source, File dest) {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {/*  ww w.  j  a  va  2  s. com*/
            try {
                inputChannel = new FileInputStream(source).getChannel();
                outputChannel = new FileOutputStream(dest).getChannel();
                outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            } finally {
                if (inputChannel != null) {
                    inputChannel.close();
                }
                if (outputChannel != null) {
                    outputChannel.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Closes InputStream and/or OutputStream. It makes sure that both streams
     * tried to be closed, even first throws an exception.
     *
     * @throw IOException if stream (is not null and) cannot be closed.
     *
     */
    protected static void close(InputStream iStream, OutputStream oStream) throws IOException {
        try {
            if (iStream != null) {
                iStream.close();
            }
        } finally {
            if (oStream != null) {
                oStream.close();
            }
        }
    }
}

Related

  1. copyFile(File in, File out)
  2. copyFile(File in, File out)
  3. copyfile(File infile, File outfile)
  4. copyFile(File inFile, File outFile)
  5. copyFile(File inputFile, File outputFile)
  6. copyFile(File source, File dest)
  7. copyFile(File source, File dest)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination)