Java FileInputStream Copy copyFile(File source, File destination)

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

Description

copy File

License

Apache License

Declaration

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

Method Source Code


//package com.java2s;
//License from project: Apache License 

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

public class Main {
    public static void copyFile(File source, File destination) throws IOException {
        if ((destination.getParentFile() != null) && (!destination.getParentFile().exists())) {

            destination.getParentFile().mkdirs();
        }//  www.ja  v  a2 s.  c o m
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {

            fis = new FileInputStream(source);
            fos = new FileOutputStream(destination);

            byte[] buffer = new byte[1024 * 4];
            int n = 0;

            while ((n = fis.read(buffer)) != -1) {
                fos.write(buffer, 0, n);
            }

        } finally {
            closeStream(fis);
            closeStream(fos);
        }
    }

    public static void closeStream(Closeable stream) {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                System.out.println("Failed to close stream !");
            }
        }
    }
}

Related

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