Java FileInputStream Copy copyFile(String src, String dest)

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

Description

Copies the specified source file to the specified destination file.

License

Apache License

Parameter

Parameter Description
src the source file to copy
dest the destination file

Exception

Parameter Description
IOException if an I/O error occurs while copying the file

Declaration

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

Method Source Code

//package com.java2s;
/**//  w w  w  .  j av  a  2s  .c  om
 * Copyright 2012 NetDigital Sweden AB
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
*/

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

public class Main {
    /**
     * Copies the specified source file to the specified destination file.
     * 
     * @param src the source file to copy
     * @param dest the destination file
     * @throws IOException if an I/O error occurs while copying the file
     */
    public static void copyFile(String src, String dest) throws IOException {
        copyFile(new File(src), new File(dest));
    }

    /**
     * Copies the specified source file to the specified destination file.
     * 
     * @param src the source file to copy
     * @param dest the destination file
     * @throws IOException if an I/O error occurs while copying the file
     */
    public static void copyFile(File src, File dest) throws IOException {
        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream(src);
            out = new FileOutputStream(dest);
            byte[] b = new byte[4096];
            int len = 0;

            while ((len = in.read(b)) > 0) {
                out.write(b, 0, len);
            }

            out.flush();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }

            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
    }
}

Related

  1. CopyFile(String sourceFilePath, String destinationFilePath)
  2. copyFile(String sourceFilePath, String destinationFilePath)
  3. copyFile(String sourcePath, String newPath)
  4. copyFile(String src, File dest)
  5. copyFile(String src, String dest)
  6. copyFile(String src, String dest)
  7. copyFile(String src, String dest)
  8. copyFile(String src, String dest)
  9. copyFile(String src, String dst)