Java FileInputStream Copy copyFile(File sourceFile, File destFile)

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

Description

copy File

License

LGPL

Declaration

public static void copyFile(File sourceFile, File destFile)
            throws IOException 

Method Source Code

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

import java.io.File;

import java.io.FileInputStream;
import java.io.FileOutputStream;

import java.io.IOException;

public class Main {
    public static void copyFile(File sourceFile, File destFile)
            throws IOException {
        if (!destFile.exists()) {
            destFile.createNewFile();//  w w w .ja v a  2 s.  co  m
        }

        FileInputStream from = null;
        FileOutputStream to = null;

        try {
            from = new FileInputStream(sourceFile);
            to = new FileOutputStream(destFile);

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead); // write
        } finally {
            if (from != null)
                try {
                    from.close();
                } catch (IOException e) {
                    ;
                }
            if (to != null)
                try {
                    to.close();
                } catch (IOException e) {
                    ;
                }
        }

    }
}

Related

  1. copyFile(File sourceFile, File destDir)
  2. copyFile(File sourceFile, File destFile)
  3. copyFile(File sourceFile, File destFile)
  4. copyFile(File sourceFile, File destFile)
  5. copyFile(File sourceFile, File destFile)
  6. copyFile(File sourceFile, File destFile)
  7. copyFile(File sourceFile, File destFile, boolean overwrite, boolean preserveLastModified)
  8. copyFile(File sourceFile, File destinationFile)
  9. copyFile(File sourceFile, File destinationFile)