Java FileInputStream Copy copyFile(File src, File dest)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File src, File dest) 

Method Source Code

//package com.java2s;
/******************************************************************************
 * Copyright (c) 2010 Oracle/*from   w  w  w.jav  a2s . c  om*/
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Original file was copied from
 * org.eclipse.wst.common.project.facet.core.util.internal.FileUtil
 *
 ******************************************************************************/

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.OutputStream;

public class Main {
    public static void copyFile(File src, File dest) {
        if (src == null || (!src.exists()) || dest == null || dest.isDirectory()) {
            return;
        }

        byte[] buf = new byte[4096];

        OutputStream out = null;
        FileInputStream in = null;

        try {
            out = new FileOutputStream(dest);
            in = new FileInputStream(src);

            int avail = in.read(buf);
            while (avail > 0) {
                out.write(buf, 0, avail);
                avail = in.read(buf);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null)
                    in.close();
            } catch (Exception ex) {
                // ignore
            }
            try {
                if (out != null)
                    out.close();
            } catch (Exception ex) {
                // ignore
            }
        }
    }
}

Related

  1. copyFile(File sourceFile, String newFileName)
  2. copyFile(File sourceLocation, File targetLocation)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dest)
  5. copyFile(File src, File dest)
  6. copyFile(File src, File dest)
  7. copyFile(File src, File dest)
  8. copyFile(File src, File dest)
  9. copyFile(File src, File dest)