Java File Copy nio copy(File src, File dst)

Here you can find the source of copy(File src, File dst)

Description

Copy the content of file src to dst TODO since java 1.7 this is provided in java.nio.file.Files

License

GNU General Public License

Parameter

Parameter Description
src a parameter
dst a parameter

Exception

Parameter Description
IOException an exception

Declaration

@SuppressWarnings("resource")
public static void copy(File src, File dst) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  w  w  w  .  j  a v a 2s.co m*/
 * BioJava development code
 *
 * This code may be freely distributed and modified under the terms of the GNU
 * Lesser General Public Licence. This should be distributed with the code. If
 * you do not have a copy, see:
 *
 * http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright for this code is held jointly by the individual authors. These
 * should be listed in @author doc comments.
 *
 * For more information on the BioJava project and its aims, or to join the
 * biojava-l mailing list, visit the home page at:
 *
 * http://www.biojava.org/
 *
 * Created on Feb 23, 2012 Created by Andreas Prlic
 *
 * @since 3.0.2
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import java.nio.channels.FileChannel;

public class Main {
    /**
     * Copy the content of file src to dst TODO since java 1.7 this is provided
     * in java.nio.file.Files
     *
     * @param src
     * @param dst
     * @throws IOException
     */
    @SuppressWarnings("resource")
    public static void copy(File src, File dst) throws IOException {

        // Took following recipe from
        // http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java
        // The nio package seems to be the most efficient way to copy a file
        FileChannel source = null;
        FileChannel destination = null;

        try {
            // we need the supress warnings here (the warning that the stream is not closed is harmless)
            // see http://stackoverflow.com/questions/12970407/does-filechannel-close-close-the-underlying-stream
            source = new FileInputStream(src).getChannel();
            destination = new FileOutputStream(dst).getChannel();
            destination.transferFrom(source, 0, source.size());
        } finally {
            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

  1. copy(File src, File dest)
  2. copy(File src, File dest)
  3. copy(File src, File dest)
  4. copy(File src, File dist)
  5. copy(File src, File dst)
  6. copy(File src, File dst)
  7. copy(File src, File dst)
  8. copy(File src, File folder)
  9. copy(File src, File target)