Java FileChannel Copy copyFile(File fileIn, File fileOut)

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

Description

copy File

License

Open Source License

Parameter

Parameter Description
fileIn a parameter
fileOut a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void copyFile(File fileIn, File fileOut) throws IOException 

Method Source Code


//package com.java2s;
//it under the terms of the GNU Affero General Public License as published by

import java.io.*;

import java.nio.channels.FileChannel;

public class Main {
    /**//from w w  w  .j a va  2 s  .  com
     * @param fileIn
     * @param fileOut
     * @throws IOException
     */
    public static void copyFile(File fileIn, File fileOut) throws IOException {
        FileChannel sourceChannel = null;
        FileChannel destinationChannel = null;
        try {
            sourceChannel = new FileInputStream(fileIn).getChannel();
            destinationChannel = new FileOutputStream(fileOut).getChannel();
            sourceChannel.transferTo(0, sourceChannel.size(), destinationChannel);
        } finally {
            try {
                if (sourceChannel != null) {
                    sourceChannel.close();
                }
            } finally {
                if (destinationChannel != null) {
                    destinationChannel.close();
                }
            }
        }
    }
}

Related

  1. copyAndReplace(File from, File to)
  2. copyChannel(ReadableByteChannel in, long length, FileChannel out)
  3. copyFile(File aFromFile, String aToFilename)
  4. copyFile(File copyFrom, File copyTo)
  5. copyFile(File file, String destDir)
  6. copyFile(File from, File to)
  7. copyFile(File from, File to)
  8. copyFile(File from, File to)
  9. copyFile(File from, File to)