Java FileInputStream Copy copyFile(File source, File destination)

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

Description

copy File

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFile(File source, File destination) throws IOException {
        ///*from   ww  w .  j a  va  2s. c  o m*/
        // if the destination is a dir, what we really want to do is create
        // a file with the same name in that dir
        //
        if (destination.isDirectory())
            destination = new File(destination, source.getName());

        FileInputStream input = new FileInputStream(source);
        copyFile(input, destination);
    }

    public static void copyFile(InputStream input, File destination) throws IOException {
        OutputStream output = null;

        output = new FileOutputStream(destination);

        byte[] buffer = new byte[1024];

        int bytesRead = input.read(buffer);

        while (bytesRead >= 0) {
            output.write(buffer, 0, bytesRead);
            bytesRead = input.read(buffer);
        }

        input.close();

        output.close();
    }
}

Related

  1. copyFile(File source, File destination)
  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 target)
  7. copyFile(File source, File target)
  8. copyFile(File source, File target)
  9. copyFile(File source, File target)