Java BufferedInputStream Copy copyFile(File inFile, File outFile)

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

Description

Copies a file.

License

Open Source License

Parameter

Parameter Description
inFile the file to copy.
outFile the copy.

Return

true if the operation succeeded completely; false otherwise.

Declaration

public static boolean copyFile(File inFile, File outFile) 

Method Source Code


//package com.java2s;
/*---------------------------------------------------------------
*  Copyright 2005 by the Radiological Society of North America
*
*  This source software is released under the terms of the
*  RSNA Public License (http://mirc.rsna.org/rsnapubliclicense)
*----------------------------------------------------------------*/

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;

import java.io.File;

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

public class Main {
    /**/*from   w w  w  .  ja  v a  2 s .  c o  m*/
     * Copies a file.
     * @param inFile the file to copy.
     * @param outFile the copy.
     * @return true if the operation succeeded completely; false otherwise.
     */
    public static boolean copyFile(File inFile, File outFile) {
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        boolean result = true;
        try {
            in = new BufferedInputStream(new FileInputStream(inFile));
            out = new BufferedOutputStream(new FileOutputStream(outFile));
            byte[] buffer = new byte[4096];
            int n;
            while ((n = in.read(buffer, 0, 4096)) != -1)
                out.write(buffer, 0, n);
        } catch (Exception e) {
            result = false;
        } finally {
            try {
                out.flush();
                out.close();
                in.close();
            } catch (Exception ignore) {
            }
        }
        return result;
    }
}

Related

  1. copyFile(File fromFile, File toFile)
  2. copyFile(File fSrcFile, File fDestFile)
  3. copyFile(File in, File out)
  4. copyFile(File in, File out)
  5. copyFile(File inFile, File outFile)
  6. copyFile(File inputFile, File outputFile)
  7. copyFile(File origem, File destino)
  8. copyFile(File source, File dest)
  9. copyFile(File source, File dest, boolean deleteIfExists)