Java FileInputStream Copy CopyFile(String sourceFilePath, String destinationFilePath)

Here you can find the source of CopyFile(String sourceFilePath, String destinationFilePath)

Description

Copies a file from one location to another.

License

Open Source License

Parameter

Parameter Description
sourceFilePath Source file path
destinationFilePath Destination file path

Exception

Parameter Description
Exception an exception

Declaration

public static void CopyFile(String sourceFilePath, String destinationFilePath) throws Exception 

Method Source Code


//package com.java2s;
// it under the terms of the GNU General Public License as published by

import java.io.*;

public class Main {
    /** Copies a file from one location to another.
     */*  w w w  .j  a  va 2 s  . co m*/
     * @param sourceFilePath Source file path
     * @param destinationFilePath Destination file path
     * @throws Exception
     */
    public static void CopyFile(String sourceFilePath, String destinationFilePath) throws Exception {
        File sourceFile = new File(sourceFilePath);
        if (sourceFile.exists()) {
            File destinationFile = new File(destinationFilePath);

            InputStream in = new FileInputStream(sourceFile);
            OutputStream out = new FileOutputStream(destinationFile);

            byte[] buf = new byte[1024];
            int len;

            while ((len = in.read(buf)) > 0)
                out.write(buf, 0, len);

            in.close();
            out.close();
        }
    }
}

Related

  1. copyFile(String sourcefile, String targetfile)
  2. copyFile(String sourcefile, String targetFile)
  3. copyFile(String sourceFile, String targetFolder)
  4. copyFile(String sourceFile, String targetFolder)
  5. copyFile(String sourceFilePath, String destinationFilePath)
  6. copyFile(String sourceFilePath, String destinationFilePath)
  7. copyFile(String sourcePath, String newPath)
  8. copyFile(String src, File dest)
  9. copyFile(String src, String dest)