Java File Copy nio fileCopy(File sourceFile, File destFile)

Here you can find the source of fileCopy(File sourceFile, File destFile)

Description

file Copy

License

Open Source License

Declaration

public static void fileCopy(File sourceFile, File destFile) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) MOBAC developers//from w ww.  j  av a 2s.  c o m
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

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

import java.nio.channels.FileChannel;

public class Main {
    public static void fileCopy(File sourceFile, File destFile) throws IOException {

        FileChannel source = null;
        FileChannel destination = null;
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream(sourceFile);
            fos = new FileOutputStream(destFile);

            source = fis.getChannel();
            destination = fos.getChannel();

            destination.transferFrom(source, 0, source.size());
        } finally {
            fis.close();
            fos.close();

            if (source != null) {
                source.close();
            }
            if (destination != null) {
                destination.close();
            }
        }
    }
}

Related

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