Java BufferedInputStream Copy copyFile(File src, String target)

Here you can find the source of copyFile(File src, String target)

Description

copy a file object;

License

Open Source License

Parameter

Parameter Description
src the source File object;
target the target file path;

Exception

Parameter Description

Declaration

public static void copyFile(File src, String target) throws FileNotFoundException, IOException 

Method Source Code


//package com.java2s;
import java.io.*;

public class Main {
    /**//from  www .j  av  a  2s . com
     * copy a file object;
     *
     * @param src    the source File object;
     * @param target the target file path;
     * @throws java.io.FileNotFoundException if the source file not found;
     * @throws java.io.IOException
     */
    public static void copyFile(File src, String target) throws FileNotFoundException, IOException {
        int read = 0;
        byte[] bytes = new byte[4096];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(src));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(target)));
        while ((read = in.read(bytes)) >= 0) {
            out.write(bytes, 0, read);
        }
        in.close();
        out.flush();
        out.close();
    }

    /**
     * copy a file from one path to another;
     *
     * @param src    the source file path;
     * @param target the target file path;
     * @throws FileNotFoundException if the source file not found;
     * @throws IOException
     */
    public static void copyFile(String src, String target) throws FileNotFoundException, IOException {
        int read = 0;
        byte[] bytes = new byte[4096];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(src)));
        BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(target)));
        while ((read = in.read(bytes)) >= 0) {
            out.write(bytes, 0, read);
        }
        in.close();
        out.flush();
        out.close();
    }
}

Related

  1. copyFile(File src, File dst)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File target)
  5. copyFile(File src, File targetDir, boolean onlyNew)
  6. copyFile(File srcFile, File destFile, boolean createCopy)
  7. copyFile(File srcFile, File detFolder)
  8. copyFile(File srcFile, File targetFolder)
  9. copyFile(final File from, final File to)