Java FileInputStream Copy copyFile(String srcPath, String dstPath)

Here you can find the source of copyFile(String srcPath, String dstPath)

Description

copy a file

License

Apache License

Parameter

Parameter Description
srcPath String the source path
dstPath String the destination path

Declaration

public static void copyFile(String srcPath, String dstPath) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;

public class Main {
    /**//from  w w  w  .j a  v a  2s.  co  m
     * copy a file
     *
     * @param srcPath String the source path
     * @param dstPath String the destination path
     */
    public static void copyFile(String srcPath, String dstPath) {
        InputStream inputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            int byteRead;
            File srcFile = new File(srcPath);

            if (srcFile.exists()) { // file exists
                inputStream = new FileInputStream(srcPath); // read the source file
                fileOutputStream = new FileOutputStream(dstPath);
                byte[] buffer = new byte[1444];
                while ((byteRead = inputStream.read(buffer)) != -1) {
                    fileOutputStream.write(buffer, 0, byteRead);
                }
            }
        } catch (Exception e) {
            System.out.println("copy file failed");
            e.printStackTrace();
        } finally {
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
                if (inputStream != null)
                    inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Related

  1. copyFile(String srcFile, String dstFile)
  2. copyFile(String srcFile, String dstFile)
  3. copyFile(String srcFileName, String destFileName)
  4. copyFile(String srcFilename, String dtsFilename)
  5. copyFile(String srcFilePath, String destFilePath)
  6. copyFile(String srcPath, String dstPath, boolean replace)
  7. copyFile(String srFile, String dtFile)
  8. copyFile(String srFile, String dtFile)
  9. copyFile(String srFile, String dtFile)