Java FileInputStream Copy copyFile(String src, String dst)

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

Description

copy File

License

Open Source License

Declaration

static public void copyFile(String src, String dst) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.*;

public class Main {
    static public void copyFile(String src, String dst) throws IOException {
        FileOutputStream fos = null;
        FileInputStream fis = null;

        try {//w w  w.j ava 2 s  .c  o m
            fos = new FileOutputStream(dst);
            fis = new FileInputStream(src);
            byte[] buf = new byte[4096];

            int nread = fis.read(buf);
            while (nread > 0) {
                fos.write(buf, 0, nread);
                nread = fis.read(buf);
            }
        } catch (IOException e) {
            File f = new File(dst);
            if (f.exists()) {
                f.delete();
            }

            throw e;
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (Exception e) {
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }

    static public boolean exists(String path) {
        File f = new File(path);
        return f.exists();
    }
}

Related

  1. copyFile(String src, String dest)
  2. copyFile(String src, String dest)
  3. copyFile(String src, String dest)
  4. copyFile(String src, String dest)
  5. copyFile(String src, String dst)
  6. copyFile(String src, String tar)
  7. copyFile(String src, String to)
  8. copyFile(String srcDir, String destDir)
  9. copyFile(String srcFile, String destFile)