Java FileInputStream Copy copyFile(File existingFile, File destFile)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File existingFile, File destFile) 

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;

public class Main {
    public static void copyFile(File existingFile, File destFile) {
        FileInputStream source = null;
        FileOutputStream destination = null;
        byte[] buffer;
        int bytes_read;

        try {/*  w  ww  . j a  va  2s  .  c o m*/
            // If we've gotten this far, then everything is okay; we can
            // copy the file.
            source = new FileInputStream(existingFile);
            destination = new FileOutputStream(destFile);
            buffer = new byte[1024];
            while (true) {
                bytes_read = source.read(buffer);
                if (bytes_read == -1)
                    break;
                destination.write(buffer, 0, bytes_read);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (source != null) {
                try {
                    source.close();
                } catch (IOException e) {
                    ;
                }
            }
            if (destination != null) {
                try {
                    destination.close();
                } catch (IOException e) {
                    ;
                }
            }
        }
    }
}

Related

  1. copyFile(File copyFrom, File copyTo)
  2. copyFile(File copyFrom, File copyTo)
  3. copyFile(File destfile, File srcfile)
  4. copyFile(File destFile, File srcFile)
  5. copyFile(File destination, File source)
  6. copyFile(File file, File destPath)
  7. copyFile(File file, File dir)
  8. copyFile(File file, File newFile, boolean overwrite, boolean setLastModified)
  9. copyFile(File file, OutputStream os)