Java FileInputStream Copy copyFile(String sourcefile, String targetFile)

Here you can find the source of copyFile(String sourcefile, String targetFile)

Description

copy File

License

Open Source License

Declaration

public static void copyFile(String sourcefile, String targetFile) throws IOException 

Method Source Code

//package com.java2s;
/**// w  w  w . j av a 2s.  co m
 * WebStorm APICloud plugin
 * Copyright (c) 2014-2015 by APICloud, Inc. All Rights Reserved.
 * Licensed under the terms of the GNU Public License (GPL) v3.
 * Please see the license.html included with this distribution for details.
 * Any modifications to this file must keep this entire header intact.
 */

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 sourcefile, String targetFile) throws IOException {
        File sourceFile = new File(sourcefile);
        if (sourceFile.exists()) {
            copyFile(sourceFile, new File(targetFile));
        }
    }

    public static void copyFile(File sourcefile, File targetFile) throws IOException {
        OutputStream out;
        InputStream in;

        in = new FileInputStream(sourcefile);
        out = new FileOutputStream(targetFile, true); //append

        byte[] b = new byte[1024]; //block read to improve performance
        int temp = 0;
        while ((temp = in.read()) != -1) {
            out.write(b, 0, temp);
        }
        in.close();
        out.close();
    }
}

Related

  1. copyFile(String source, String targetDirectory, String filesep)
  2. copyFile(String sourceFile, String destDir, String newFileName)
  3. copyFile(String sourceFile, String destFile)
  4. copyFile(String sourceFile, String targetFile)
  5. copyFile(String sourcefile, String targetfile)
  6. copyFile(String sourceFile, String targetFolder)
  7. copyFile(String sourceFile, String targetFolder)
  8. copyFile(String sourceFilePath, String destinationFilePath)
  9. CopyFile(String sourceFilePath, String destinationFilePath)