Java FileInputStream Copy copyFile(File src, File dest)

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

Description

This method copies the content of a file (possibly directory) in another file.

License

Open Source License

Parameter

Parameter Description
src the source file that will be copied.
dest the destination file, result of the copy.

Exception

Parameter Description

Declaration

public static void copyFile(File src, File dest) throws IOException 

Method Source Code

//package com.java2s;
/**//from   w  w w .j av  a 2s . c o m
 * AADL-RAMSES
 * 
 * Copyright ? 2014 TELECOM ParisTech and CNRS
 * 
 * TELECOM ParisTech/LTCI
 * 
 * Authors: see AUTHORS
 * 
 * This program is free software: you can redistribute it and/or modify 
 * it under the terms of the Eclipse Public License as published by Eclipse,
 * either version 1.0 of the License, or (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * Eclipse Public License for more details.
 * You should have received a copy of the Eclipse Public License
 * along with this program.  If not, see 
 * http://www.eclipse.org/org/documents/epl-v10.php
 */

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

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    /**
     * This method copies the content of a file (possibly directory)
     * in another file. Both must be created before calling this 
     * method.
     * @param src the source file that will be copied.
     * @param dest the destination file, result of the copy.
     * @throws for any IO problems
     */
    public static void copyFile(File src, File dest) throws IOException {
        InputStream in = null;
        OutputStream out = null;

        if (dest.isDirectory())
            dest = new File(dest, src.getName());

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

            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
        } finally {
            if (in != null) {
                in.close();
            }

            if (out != null) {
                out.close();
            }
        }
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dest)
  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, int bufSize)