Java File Copy nio fileCopy(File src, File dest)

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

Description

file Copy

License

Open Source License

Parameter

Parameter Description
src a parameter
dest a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void fileCopy(File src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Manchester Centre for Integrative Systems Biology
 * University of Manchester//from ww  w  .  j  a  v  a2 s . com
 * Manchester M1 7ND
 * United Kingdom
 * 
 * Copyright (C) 2007 University of Manchester
 * 
 * This program is released under the Academic Free License ("AFL") v3.0.
 * (http://www.opensource.org/licenses/academic.php)
 *******************************************************************************/

import java.io.*;

import java.nio.channels.*;

public class Main {
    /**
     * 
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void fileCopy(File src, File dest) throws IOException {
        if (!dest.exists()) {
            final File parent = new File(dest.getParent());

            if (!parent.exists() && !parent.mkdirs()) {
                throw new IOException();
            }

            if (!dest.exists() && !dest.createNewFile()) {
                throw new IOException();
            }
        }

        try (final FileInputStream is = new FileInputStream(src);
                final FileOutputStream os = new FileOutputStream(dest);
                final FileChannel srcChannel = is.getChannel();
                final FileChannel dstChannel = os.getChannel()) {
            dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
        }
    }

    /**
     * 
     * @param src
     * @param dest
     * @throws IOException
     */
    public static void fileCopy(String src, String dest) throws IOException {
        fileCopy(new File(src), new File(dest));
    }
}

Related

  1. copyToTempFile(final InputStream is, final String prefix, final String suffix)
  2. copyToTmpFile(InputStream in, String prefix, String suffix)
  3. fileCopy(File source, File destination)
  4. fileCopy(File source, File target)
  5. fileCopy(File sourceFile, File destFile)
  6. fileCopy(final File dest, final File src)