Java FileInputStream Copy copyFile(File src, File dest)

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

Description

Copy a file to another location.

License

Open Source License

Parameter

Parameter Description
src the source file.
dest the destination.

Return

true if the file was successfully copied, false otherwise.

Declaration

public static boolean copyFile(File src, File dest) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2000, 2009 IBM Corporation and others.
 * 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
 *
 * Contributors://from  ww w. jav  a2 s .c om
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * Copy a file to another location.
     *
     * @param src the source file.
     * @param dest the destination.
     * @return <code>true</code> if the file was successfully copied,
     *    <code>false</code> otherwise.
     */
    public static boolean copyFile(File src, File dest) {

        try {
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

Related

  1. copyFile(File sourceFile, File targetFile)
  2. copyFile(File sourceFile, String newFileName)
  3. copyFile(File sourceLocation, File targetLocation)
  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)