Java File Copy nio copyFile(final File source, final File dest)

Here you can find the source of copyFile(final File source, final File dest)

Description

Copia un fichero.

License

Open Source License

Parameter

Parameter Description
source Fichero origen con el contenido que queremos copiar.
dest Fichero destino de los datos.

Exception

Parameter Description
IOException SI ocurre algun problema durante la copia

Declaration

public static void copyFile(final File source, final File dest)
        throws IOException 

Method Source Code

//package com.java2s;
/* Copyright (C) 2011 [Gobierno de Espana]
 * This file is part of "Cliente @Firma".
 * "Cliente @Firma" 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.
 *   - or The European Software License; either version 1.1 or (at your option) any later version.
 * Date: 11/01/11/*from   w  ww  .j  a v a2s . c  o  m*/
 * You may contact the copyright holder at: soporte.afirma5@mpt.es
 */

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

import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    /** Copia un fichero.
     * @param source Fichero origen con el contenido que queremos copiar.
     * @param dest Fichero destino de los datos.
     * @throws IOException SI ocurre algun problema durante la copia */
    public static void copyFile(final File source, final File dest)
            throws IOException {
        if (source == null || dest == null) {
            throw new IllegalArgumentException(
                    "Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$
        }

        final FileInputStream is = new FileInputStream(source);
        final FileOutputStream os = new FileOutputStream(dest);
        final FileChannel in = is.getChannel();
        final FileChannel out = os.getChannel();
        final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY,
                0, in.size());
        out.write(buf);

        in.close();
        out.close();
        is.close();
        os.close();

    }
}

Related

  1. copy(File toFile, File fromFile)
  2. copyFile(File f1, File f2)
  3. copyFile(File source, File dest, boolean visibleFilesOnly)
  4. copyFile(File src, File dest)
  5. copyFile(FileChannel srcFc, File dstFile)
  6. copyFile(final File source, final File target)
  7. copyFile(final File source, final File target)
  8. copyFileByMapped(String sourcePath, String targetPath)
  9. copyToFile(final InputStream is, final File file)