Java BufferedInputStream Copy copyFile(File src, File dst)

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

Description

copy File

License

Apache License

Declaration

public static boolean copyFile(File src, File dst) 

Method Source Code


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

import java.io.*;

public class Main {
    public static boolean copyFile(File src, File dst) {
        if (!src.exists()) {
            return false;
        }//  ww  w .ja v  a 2  s.c  o m
        if (!dst.getParentFile().exists()) {
            dst.getParentFile().mkdirs();
        } else {
            if (dst.exists()) {
                dst.delete();
            }
        }
        try {
            dst.createNewFile();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        BufferedInputStream in = null;
        BufferedOutputStream out = null;

        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(dst));
            byte[] buffer = new byte[1024 * 4];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            out.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                }
            }
            if (out != null) {
                try {
                    out.close();
                } catch (IOException e) {
                }
            }
        }
        return true;
    }
}

Related

  1. copyFile(File src, File dest)
  2. copyFile(File src, File dest)
  3. copyFile(File src, File dest)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File target)
  7. copyFile(File src, File targetDir, boolean onlyNew)
  8. copyFile(File src, String target)
  9. copyFile(File srcFile, File destFile, boolean createCopy)