Java FileChannel Copy copyFile(String origPath, String destPath)

Here you can find the source of copyFile(String origPath, String destPath)

Description

Convenience function to copy a file.

License

Open Source License

Parameter

Parameter Description
origPath Original path
destPath Destination path

Exception

Parameter Description
IOException If the operation fails during the data copy phase or,FileNotFoundException if either file exists but is a directoryrather than a regular file, does not exist but cannot be read/created,or cannot be opened for any other reason.

Declaration

public static void copyFile(String origPath, String destPath) throws IOException 

Method Source Code

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

import java.io.FileInputStream;

import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**/*from   www .  ja  va  2  s  . c om*/
     * Convenience function to copy a file.
     * <p>
     * If the destination file exists it is overwritten.
     * 
     * @param origPath Original path
     * @param destPath Destination path
     * @throws IOException If the operation fails during the data copy phase or,
     *             {@link FileNotFoundException} if either file exists but is a directory
     *             rather than a regular file, does not exist but cannot be read/created,
     *             or cannot be opened for any other reason.
     */
    public static void copyFile(String origPath, String destPath) throws IOException {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream(origPath);
            fos = new FileOutputStream(destPath);
            final FileChannel in = fis.getChannel();
            final FileChannel out = fos.getChannel();
            in.transferTo(0, in.size(), out);
        } finally {
            if (fis != null)
                fis.close();
            if (fos != null)
                fos.close();
        }
    }
}

Related

  1. copyFile(String in, String out)
  2. copyFile(String infile, String outfile)
  3. copyFile(String inFile, String outFile)
  4. copyFile(String inName, String otName)
  5. copyFile(String input, String output)
  6. copyFile(String source, String destination)
  7. copyFile(String source, String destination)
  8. copyFile(String source, String destination)
  9. copyFile(String source, String target)