Java FileInputStream Copy copyFile(File srcPath, File dstPath)

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

Description

copy File

License

Open Source License

Declaration

public static void copyFile(File srcPath, File dstPath) throws FileNotFoundException, IOException 

Method Source Code

//package com.java2s;
//License from project: GNU General Public License 

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class Main {
    public static void copyFile(File srcPath, File dstPath) throws FileNotFoundException, IOException {
        InputStream in = new FileInputStream(srcPath);
        OutputStream out = new FileOutputStream(dstPath);
        copy(in, out);//from  ww  w .java  2s  .  c  o m
        in.close();
        out.close();
    }

    public static void copyFile(byte[] data, File dstPath) throws FileNotFoundException, IOException {
        InputStream in = new ByteArrayInputStream(data);
        OutputStream out = new FileOutputStream(dstPath);
        copy(in, out);
        in.close();
        out.close();
    }

    private static void copy(InputStream in, OutputStream out) throws IOException {
        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0) {
            out.write(buf, 0, len);
        }
    }
}

Related

  1. copyFile(File srcFile, File destFile, boolean recursive)
  2. copyFile(File srcFile, File dstFile)
  3. copyFile(File srcFile, File dstFile)
  4. copyFile(File srcFile, File dstFile)
  5. copyFile(File srcFile, File dstFile)
  6. copyfile(File srFile, File dtFile)
  7. copyFile(File target, File source)
  8. copyFile(File targetFile, File file)
  9. copyFile(File targetFile, File file)