Java FileInputStream Copy copyFile(File source, File destination)

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

Description

Copies the source file to the destination filename.

License

Open Source License

Declaration

public static void copyFile(File source, File destination) throws IOException 

Method Source Code


//package com.java2s;
/*//w w w  .  j  av a 2 s .  c  om
 * Copyright (c) 2016 Vivid Solutions.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * and Eclipse Distribution License v. 1.0 which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 *
 * http://www.eclipse.org/org/documents/edl-v10.php.
 */

import java.io.*;

public class Main {
    /**
     * Copies the source file to the destination filename.
     * Posted by Mark Thornton <mthorn@cix.compulink.co.uk> on Usenet.
     */
    public static void copyFile(File source, File destination) throws IOException {
        RandomAccessFile out = new RandomAccessFile(destination, "rw");
        //Tell the OS in advance how big the file will be. This may reduce fragmentation
        out.setLength(source.length());
        //copy the content
        FileInputStream in = new FileInputStream(source);
        byte[] buffer = new byte[16384];
        while (true) {
            int n = in.read(buffer);
            if (n == -1)
                break;
            out.write(buffer, 0, n);
        }
        in.close();
        out.close();
    }
}

Related

  1. copyFile(File source, File dest)
  2. copyFile(File source, File destination)
  3. copyFile(File source, File destination)
  4. copyFile(File source, File destination)
  5. copyFile(File source, File destination)
  6. copyFile(File source, File destination)
  7. copyFile(File source, File destination)
  8. copyFile(File source, File destination)
  9. copyFile(File source, File destination)