Java FileInputStream 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 void copyFile(File src, File dst) 

Method Source Code

//package com.java2s;
/**//ww w  .  j a  v  a  2  s  .c  om
 *  Copyright (c) 2014 http://www.lushapp.wang
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 */

import java.io.*;

public class Main {

    public static void copyFile(File src, File dst) {
        try {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                out = new FileOutputStream(dst);
                in = new FileInputStream(src);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = in.read(buffer)) > 0) {
                    out.write(buffer, 0, len);
                }
            } catch (Exception e) {
                e.printStackTrace();
                String dstpath = dst.getAbsolutePath();
                if (dstpath.lastIndexOf("/") != -1) {
                    dstpath = dstpath.subSequence(0, dstpath.lastIndexOf("/")).toString();
                } else {
                    dstpath = dstpath.subSequence(0, dstpath.lastIndexOf("\\")).toString();
                }
                createDirectory(dstpath);
                copyFile(src, dst);
            } finally {
                if (null != in) {
                    in.close();
                }
                if (null != out) {
                    out.close();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void createDirectory(String Directorypath) {
        File file = new File(Directorypath);
        if (!file.exists()) {
            file.mkdir();
            file.mkdirs();
        }
    }
}

Related

  1. copyFile(File src, File dest, int bufSize)
  2. copyFile(File src, File dst)
  3. copyFile(File src, File dst)
  4. copyFile(File src, File dst)
  5. copyFile(File src, File dst)
  6. copyFile(File src, File dst)
  7. copyFile(File src, File dst)
  8. copyFile(File src, File dst)
  9. copyFile(File src, File dst)