Java File Copy nio copy(File source, File target)

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

Description

Copy one file to another

License

Open Source License

Parameter

Parameter Description
source the file to be read
target the file to be written

Declaration

public static void copy(File source, File target) throws IOException 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

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 {
    /**//from w  ww.j av  a2  s. c o  m
     * Copy one file to another
     *
     * @param source the file to be read
     * @param target the file to be written
     *
     * @exception IOException raised if operation fails
     */
    public static void copy(File source, File target) throws IOException {
        FileChannel input = null;
        FileChannel output = null;

        try {
            input = new FileInputStream(source).getChannel();
            output = new FileOutputStream(target).getChannel();

            MappedByteBuffer buffer = input.map(FileChannel.MapMode.READ_ONLY, 0, input.size());
            output.write(buffer);
        } finally {
            if (input != null) {
                input.close();
            }

            if (output != null) {
                output.close();
            }
        }
    }
}

Related

  1. copy(File source, File destination)
  2. copy(File source, File destination)
  3. copy(File source, File destination)
  4. copy(File source, File destination)
  5. copy(File source, File destination)
  6. copy(File source, File target)
  7. copy(File source, File target)
  8. copy(File source, File target, FilenameFilter filter)
  9. copy(File sourceFile, File destFile)