Java FileInputStream Copy copyFile(String srcFilePath, String destFilePath)

Here you can find the source of copyFile(String srcFilePath, String destFilePath)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String srcFilePath, String destFilePath) throws IOException 

Method Source Code


//package com.java2s;
/* Copyright 2012, 2013 Unconventional Thinking
 *
 * This file is part of Hierarchy.//w w  w.j a  va 2  s .com
 *
 * Hierarchy is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License 
 * as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * Hierarchy 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 GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with Hierarchy.  
 * If not, see <http://www.gnu.org/licenses/>.
 */

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 {
    public static void copyFile(String srcFilePath, String destFilePath) throws IOException {

        File srcFile = new File(srcFilePath);
        File destFile = new File(destFilePath);

        copyFile(srcFile, destFile);
    }

    public static void copyFile(File srcFile, File destFile) throws IOException {
        if (srcFile == null) {
            throw new IOException("the path to the src file is null.");
        }
        if (!srcFile.exists()) {
            throw new IOException("For the move operation, the path to the src file does not exist: "
                    + srcFile.getAbsolutePath());
        }

        InputStream in = new FileInputStream(srcFile);

        //For Overwrite the file.
        OutputStream out = new FileOutputStream(destFile);

        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 srcFile, String destFile)
  2. copyFile(String srcFile, String dstFile)
  3. copyFile(String srcFile, String dstFile)
  4. copyFile(String srcFileName, String destFileName)
  5. copyFile(String srcFilename, String dtsFilename)
  6. copyFile(String srcPath, String dstPath)
  7. copyFile(String srcPath, String dstPath, boolean replace)
  8. copyFile(String srFile, String dtFile)
  9. copyFile(String srFile, String dtFile)